codeart
codeart

Reputation: 3

PHP/MYSQL duplicating last row on insert statement

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

Answers (3)

regilero
regilero

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

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

You're running the query twice. Try this:

$result = mysql_query( $sql,$con );

if (!$result) {...

Upvotes: 2

Oli
Oli

Reputation: 2452

if (!mysql_query($sql,$con)) executes the query again.

Should be:

$result = mysql_query( $sql,$con );

if (!$result)

Upvotes: 3

Related Questions