Forum Moderators: coopster

Message Too Old, No Replies

Add to table with fk

add data to multiple tables with fk

         

Able Net Design

8:45 am on Dec 18, 2003 (gmt 0)

10+ Year Member



ok the main problem i am having is not the adding of data to a table but the retrieveing of a id from another table.

the error is get is "Invalid query: Duplicate entry '0' for key 2"

this is the code i am using the first part is to get the user id from the table user where the company matches. Then the second part is to add that data to the table.

$result2 = mysql_query("SELECT user_id FROM user WHERE company = '$company' AND auth_level = '1'") or die("Invalid query: " . mysql_error());
while($row=mysql_fetch_array($result2))
{
$user = $row["user_id"];
$result=MYSQL_QUERY("INSERT INTO `invoice` (user_id,totalcost,gst,unitprice,domain,duedate,paid)" . "VALUES('$user','$totalprice','$gst','$unitprice','$domain','$duedate','$paid')") or die("Invalid query: " . mysql_error());

}

[edited by: jatar_k at 4:46 pm (utc) on Dec. 18, 2003]
[edit reason] fixed sidescroll [/edit]

jatar_k

4:45 pm on Dec 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try a couple of things

echo the value of $user after the first query and compare it against the proper value in the table to make sure that is working.

Then take a look at the data in the second table, maybe the key is already there. You may have to do an "if not in insert else update" scenario.

I know the error is right so now you need to look at why and where it's coming from.

Another good tip is to echo your constructed sql queries before sending them off to mysql_query.