tags:

views:

43

answers:

2

Insert into two tables from a single form. The first insert go in fine the second generates this error Duplicate entry '0' for key 1 any idea what is happening?

$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
$db=mysql_select_db ("database", $connection) or die (mysql_error());
$query = "INSERT INTO worklog (id, newtime, datetime, clientname, clientcode, startmo, startday, startyr, endmo, endday, endyr, duemo, dueday, dueyr, market, job, allTypes, spec, status, designer, dsgnemail, adrep, ademail, frame1, frame2, frame3, rush) VALUES ('$id', $newtime, now(), '$clientname', '$clientcode', '$startmo', '$startday', '$startyr', '$endmo', '$endday', '$endyr', '$duemo', '$dueday', '$dueyr', '$market', '$job', '$allTypes', '$spec', '$status', '$designer', '$dsgnemail', '$adrep', '$ademail', '$frame1', '$frame2', '$frame3', '$rush')";
$sql_result = mysql_query($query, $connection) or die (mysql_error());

$worklog_id=mysql_insert_id($connection); 

$connection2=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
$db2=mysql_select_db ("database", $connection2) or die (mysql_error());
$query2 = "INSERT INTO worklognotes (worklog_id, spec) VALUES ('$worklog_id', '$spec')";

$sql_result = mysql_query($query2, $connection2) or die (mysql_error());    
+1  A: 

normally with and table ID column you set it to auto-increment and never explicitly insert it. The database management system will take care of inserting that column. The error means that you are inserting a row that has that ID already, meaning the column has a UNIQUE constraint.

Scott M.
@Scott, I don't think that's the issue. It looks like the ID in the second insert is a FK to the first insert, or at least should be. The problem is that he doesn't seem to be getting the PK back from the first insert to use. Granted, the first insert is setting the ID explicitly, so your point remains valid, but that's not where the error is happening.
David
the error says that there is a duplicate entry for "key 1" which i am assuming is the foreign key. Normal MySQL databases use the MyISAM engine, which does not have support for foreign keys. if a UNIQUE constraint was placed on the column, it could throw this error.
Scott M.
Why is auto-incremental working in the first insert and not in the second. The "id" in both tables are set to auto-incremental and primary key
KingMob
because you are explicitly setting the ID in the second query, and that is causing a conflict because the ID is already in the table. if it is a foreign key you should not have it as auto-increment. In fact, MyISAM (if you are using it) doesn't explicitly support foreign keys. You would need InnoDB for that.
Scott M.
So I don't need the id column in the second table?These are the columns in the second table.id, worklog_id, timestamp, notes
KingMob
no, i wouldnt say that. I always have an ID column in every table. It's good to have a unique value for every row. I'm saying the worklog_id you are adding is already there.
Scott M.
Removing the id column fixed the problem
KingMob
+3  A: 

I thin the culprit is the line:

$worklog_id=mysql_insert_id($connection); 

according to the PHP documentation:

"The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established"

So if the id in worklog is not AUTO_INCREMENT it will always return 0 ... your second attempt at running the code will cause:

Duplicate entry '0' for key 1

Two ways to fix this:

  1. id for worklog should be AUTO_INCREMENT ... this way mysql_insert_id will return ther ID generated by the database and you can use it as a working id for the next query

  2. just use $id instead of $worklog_id

jsshah
I deleted the table and rebuild it. The first insert went fine the id from the first insert is going into the second insert in $worklog_id. The id for the second table is 0 and won't autoadvance. Any other ideas?
KingMob
can you also share the script / sql snippets you are using to create the tables
jsshah