Mr Chris Posted April 5, 2006 Share Posted April 5, 2006 Hi Guys,Trying to add data to a DB via a form, but getting the error message:[b]Query failed: Duplicate entry '' for key 1[/b]Looked for duplicate data in my form, but can't see anything - can anyone please advise?Thanks[code]<html><head><title>Add Details to Database</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body bgcolor="#FFFFFF"><?php $Host = "localhost";$User = "******";$Password = "*****";$DBname = "******";$TableName = "student";$Link = mysql_connect($Host,$User, $Password); $Result = mysql_db_query($DBname,$Query,$Link);if(isset($_POST['Submit'])){ $query = "INSERT INTO student VALUES ('$studentid','$password','$dob','$firstname','$lastname','$house','$town','$county','$country','$postcode')"; $msg = "New record is saved"; $Link = mysql_connect($Host,$User, $Password); mysql_select_db($DBname); $result = mysql_query($query) or die('Query failed: ' . mysql_error()); mysql_close(); } ?> <?php echo $msg?> <form action="" method="post"> Student ID: <input type="text" name="studentid"> <br> Password: <input type="text" name="password"> <br> Date of Birth: <input type="text" name="dob"> <br> First Name: <input type="text" name="firstname"> <br> Last Name: <input type="text" name="lastname"> <br> House: <input type="text" name="house"> <br> Town: <input type="text" name="town"> <br> County: <input type="text" name="county"> <br> Country: <input type="text" name="country"> <br> Postcode: <input type="text" name="postcode"> <br> <input type="Submit" name="Submit" value="Submit"></form></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted April 5, 2006 Share Posted April 5, 2006 Explicitly name the fields your inserting into and this problem should become clear, or go away. eg..[code]INSERT INTO tbl (fld1,fld2,fl3) VALUES ('$v1','$v2','$v3');[/code]Is the field your trying to insert $studentid into and auto increment field? Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted April 5, 2006 Author Share Posted April 5, 2006 Hi Thorpe,$studentid is set to be a primary key, but the student will specify their own ID in the form. So should that hopefully work? Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted April 6, 2006 Author Share Posted April 6, 2006 [DELETED BY CHRIS] Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted April 6, 2006 Author Share Posted April 6, 2006 Hi All,This is driving me potty - still getting the same error! [b]Query failed: Duplicate entry '' for key 1[/b]Here's my db layout:(employeeid varchar(8) not null,password varchar(6) not null,dob date not null,firstname varchar(20) not null,lastname varchar(20) not null,house varchar(30) not null,town varchar(30) not null,county varchar(30) not null,country varchar(30) not null,postcode varchar(10) not null,PRIMARY KEY (employeeid)); - //Not AN AUTOINCREMENT...And my codeCan you please advise?[code]<?php $Host = "localhost";$User = "********";$Password = "*******";$DBname = "*******";$TableName = "******";$Link = mysql_connect($Host,$User, $Password); $Result = mysql_db_query($DBname,$Query,$Link);if(isset($_POST['Submit'])){ $query = "INSERT INTO employee (employeeid,password,dob,firstname,lastname,house,town,county,country,postcode) VALUES ('$employeeid','$password','$dob','$firstname','$lastname','$house','$town','$county','$country','$postcode')"; $msg = "New record is saved"; $Link = mysql_connect($Host,$User, $Password); mysql_select_db($DBname); $result = mysql_query($query) or die('Query failed: ' . mysql_error()); mysql_close(); } ?> <html> <head> <title>Add Details to Database</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF"><?php echo $msg?><form action="" method="post"> Employee ID: <input type="text" name="employeeid"><br> Password: <input type="text" name="password"><br> Date of Birth: <input type="text" name="dob"><br> First Name: <input type="text" name="firstname"><br> Last Name: <input type="text" name="lastname"><br> House: <input type="text" name="house"><br> Town: <input type="text" name="town"><br> County: <input type="text" name="county"><br> Country: <input type="text" name="country"><br> Postcode: <input type="text" name="postcode"><br> <input type="Submit" name="Submit" value="Submit"> </form> </body> </html> [/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.