Reputation: 3
I am getting an unwanted duplicate entry for every last row on an insert statement. Does anyone know why this happens and how I can fix it?
?php
if(isset($_POST['submit'])) {
$con = mysql_connect("localhost"," "," ");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$sql="INSERT INTO table(ID,user) VALUE('$ID','$_POST[user]')";
$result = mysql_query( $sql,$con );
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
header( 'Location: index.php?success' ) ;
}
?>
Upvotes: 0
Views: 505
Reputation: 30496
And please sanitize the $_POST before using it ine a query string (mysql_real_escape at least). Maybe you could comment somewhere what is $ID and how you get it.
Upvotes: 0
Reputation: 124768
You're running the query twice. Try this:
$result = mysql_query( $sql,$con );
if (!$result) {...
Upvotes: 2
Reputation: 2452
if (!mysql_query($sql,$con)) executes the query again.
Should be:
$result = mysql_query( $sql,$con ); if (!$result)
Upvotes: 3