Jump to content

Archived

This topic is now archived and is closed to further replies.

Mr Chris

Form trouble adding data to db

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 code

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.