By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.


P: 5
I get the error of Duplicate entry '' for key 1 when running a mysql query
this comes from a form that has textboxes and then tries to save to student table in mysql.

Expand|Select|Wrap|Line Numbers
  1. $id=mysql_real_escape_string($_POST['txtstudentid']);
  2. $password=mysql_real_escape_string($_POST['txtpassword']);
  3. $dob=mysql_real_escape_string($_POST['txtdob']);
  4. $firstname=mysql_real_escape_string($_POST['txtfirstname']);
  5. $lastname=mysql_real_escape_string($_POST['txtlastname']);
  6. $house=mysql_real_escape_string($_POST['txthouse']);
  7. $town=mysql_real_escape_string($_POST['txttown']);
  8. $county=mysql_real_escape_string($_POST['txtcounty']);
  9. $country=mysql_real_escape_string($_POST['txtcountry']);
  10. $postcode=mysql_real_escape_string($_POST['txtpostcode']);
  14.     // Build an sql statment to add the student details
  15.       $sql="INSERT INTO student(studentid,password,dob,firstname,lastname,house,town,county,country,postcode) VALUES('$id','$password','$dob','$firstname','$lastname','$house','$town','$county','$country','$postcode')";
  16.     $result = mysql_query($sql,$conn);
  17.         if($result){ 
  18.   echo"<br/>Your details have been updated"; 
  19.   echo "<BR>"; 
  20.   echo "<a href='Home.html'>Back to main page</a>"; 
  21. } else { 
  22.   echo "There has been an error <br/>"; 
  23.   print mysql_error(); 
  26. // close connection 
  27. mysql_close($conn);
  28. ?>
Mar 6 '12 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 10K+
P: 12,365
The error is saying that you're trying to insert an id that already exists in the table.
Mar 6 '12 #2

P: 5
actually, i tried to add different ids into the table but i still get the error message
Mar 7 '12 #3

Expert Mod 10K+
P: 12,365
Are you sure? Did you output the string to check?
Mar 7 '12 #4

P: 5
yes, if it helps i read somewhere that it might be to do with an outdated version of php? I am using Notepad to write my php code.
Mar 7 '12 #5

Expert Mod 10K+
P: 12,365
That may be the case, but I doubt it. Please post the output of the SQL string that causes the error.
Mar 7 '12 #6

P: 5
thanks for your help, but i have it sorted now, by adding this to where i insert the data into the form. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
Mar 7 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.