Jump to content

Insert problem with forms


jackie11

Recommended Posts

Hi

I am having problem inserting data into my db via a form, I am getting the message up:

Duplicate entry '' for key 1


The code I'm using is:

[code]<?php
$user="root";
$host="localhost";
$password="";
$database = "employee_index";
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("employee_index") or die(mysql_error());

$sqlquery = "INSERT INTO employee (Employee_ID, Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture)
VALUES ('$_post[Employee_ID]','$_post[Surname]','$_post[Forename]','$_post[Job_title]','$_post[Office_location]','$_post[Telephone]','$_post[Email]','$_post[Expertise]','$_post[Hobbies]','$_post[DOB]','$_post[Picture]')";

$result = mysql_query($sqlquery)
or die (mysql_error());

?>

[/code]

I have tried a number of different ways to set this up but none of them are working

Can anyone see where I am going wrong? :'(

Jackie
Link to comment
https://forums.phpfreaks.com/topic/34823-insert-problem-with-forms/
Share on other sites

The error actually refers to your data.

You are manually entering in data for your 'primary key'.  The primary key must be unique.  Most people set their primary key as auto_increment and then don't assign it a value at all.. the database takes care of that by the first record inserted is 1 second is 2 and so on....

Check your primary key... then check the record you are inserting.  You'll notice there is a record that already exists and the value of the primary key exists.
Hi

Thanks for the reply, I have tried this and changed my Employee_ID to an auto increment and removed this field from the form, the problem is now when I add in details into the form and press submit the db creates a new row but does not forward the information I have added on the form, any suggestions! :-\

Jackie

Archived

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

×
×
  • Create New...

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.