Jump to content


Photo

Form trouble adding data to db


  • Please log in to reply
4 replies to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 05 April 2006 - 03:40 PM

Hi Guys,

Trying to add data to a DB via a form, but getting the error message:

Query failed: Duplicate entry '' for key 1

Looked for duplicate data in my form, but can't see anything - can anyone please advise?

Thanks

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


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 April 2006 - 03:48 PM

Explicitly name the fields your inserting into and this problem should become clear, or go away. eg..
INSERT INTO tbl (fld1,fld2,fl3) VALUES ('$v1','$v2','$v3');
Is the field your trying to insert $studentid into and auto increment field?

#3 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 05 April 2006 - 03:59 PM

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?

#4 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 06 April 2006 - 07:40 AM

[DELETED BY CHRIS]

#5 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 06 April 2006 - 08:30 AM

Hi All,

This is driving me potty - still getting the same error!

Query failed: Duplicate entry '' for key 1

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 code

Can you please advise?



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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users