php/mysql – PHP

  php

Q(Question):

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.

$id=mysql_real_escape_string($_POST['txtstudentid']);
$password=mysql_real_escape_string($_POST['txtpassword']);
$dob=mysql_real_escape_string($_POST['txtdob']);
$firstname=mysql_real_escape_string($_POST['txtfirstname']);
$lastname=mysql_real_escape_string($_POST['txtlastname']);
$house=mysql_real_escape_string($_POST['txthouse']);
$town=mysql_real_escape_string($_POST['txttown']);
$county=mysql_real_escape_string($_POST['txtcounty']);
$country=mysql_real_escape_string($_POST['txtcountry']);
$postcode=mysql_real_escape_string($_POST['txtpostcode']);
// Build an sql statment to add the student details
$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')";
$result = mysql_query($sql,$conn);
if($result){
echo"<br/>Your details have been updated";
echo "<BR>";
echo "<a href='Home.html'>Back to main page</a>";
} else {
echo "There has been an error <br/>";
print mysql_error();
}
// close connection
mysql_close($conn);
?>

A(Answer):

The error is saying that you’re trying to insert an id that already exists in the table.

A(Answer):

@Rabbit

actually, i tried to add different ids into the table but i still get the error message

A(Answer):

Are you sure? Did you output the string to check?

A(Answer):

@Rabbit

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.

A(Answer):

That may be the case, but I doubt it. Please post the output of the SQL string that causes the error.

A(Answer):

@Rabbit

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">

LEAVE A COMMENT