0

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' ) ;
}
?>
1
  • 1
    As @oli and @Tatu Ulmanen state, you're running this query twice. However, you might also want to consider specifying a unique compound key in your database table so that it's not possible to have duplicate entries. Commented Dec 23, 2010 at 11:09

3 Answers 3

3

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

Should be:

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

if (!$result)
Sign up to request clarification or add additional context in comments.

Comments

2

You're running the query twice. Try this:

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

if (!$result) {...

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.