PDA

View Full Version : insert into 2 tables


mwm
02-19-2007, 03:11 PM
I need to do an insert into 2 tables. I need the id in both. I have tried several things and can't get it to work. I thought someone might tell what I"m doing wrong.


$query = "INSERT INTO contacts (id,fname,lname, title,company,address,city, state, zip, bphone, fax, mobile, email, category,ophone,website,haddress,hcity,hstate,hzip,hphone,hcell,hfax,hother,bday,spouse) values('$id','$fname','$lname','$title','$company', '$address','$city','$state','$zip','$bphone','$fax','$mobile','$email','$category','$ophone','$websi te','$haddress','$hcity','$hstate','$hzip','$hphone','$hcell','$hfax','$hother','$bday','$spouse')";


$result = mysql_query($query) or die("Could not insert");



$query1 = "INSERT INTO note_tbl(noteid,id,note)values('$noteid','{$_POST[id]}','$note')";


$result1 = mysql_query($query1) or die("Could not insert");

Nightfire
02-19-2007, 03:13 PM
What errors are you getting? At quick glance I see nothing wrong. Try changing the

or die("Could not insert");

to

or die(mysql_error());

mwm
02-19-2007, 03:37 PM
Not getting any errors but when I look at the note_tbl the id is 0 not the id that is inserted into the contacts tables.

Nightfire
02-19-2007, 04:04 PM
try this

$query1 = "INSERT INTO note_tbl(noteid,id,note)values('".$noteid."','".$_POST['id']."','".$note."')";

mwm
02-19-2007, 04:11 PM
Thanks but no help, still get a 0 in the note_tbl. Do you thing that since this is a new contact and the id is not inserted until the info is saved. would i need to do some kink of select id, and then insert that into the note_tbl?

Nightfire
02-19-2007, 05:17 PM
I'm lost now. Are you trying to add an id no/ without knowing what the id no/ is?

aedrin
02-19-2007, 06:59 PM
$_POST[id]

This is BAD. Use this:

$_POST['id']

Develop with display_errors set to true, and error_reporting set to E_ALL & E_NOTICE.

In strings, using {} (like you did) prevents PHP from messing up, so you don't have to worry about that.

Problem: Both of your tables have an 'id' column. After inserting a record in both tables, note_tbl does not contain the proper value in the 'id' column (always 0).

Is this problem statement correct?

Issues:

Your first query uses the $id variable for its 'id' column, the second query uses $_POST['id']. This could be a reason that your queries are not inserting the same value, although I don't see how you could know the ID of a record before inserting it.

It is usually better to keep 'id' reserved for the primary key, and make it automatic by giving it the 'auto_increment' property. This means you do not insert values into it directly. This guarantees that the 'id' column is unique.

If you need to have some sort of generated unique identifier, seperate it from the 'id' column. (For identifiers like 'N-00032', etc.)

If you are already using auto_increment, then remove the id column from the first insert statement, then in the second do something like this:


$sql = "INSERT INTO notes(id, contact_id, note) VALUES(NULL, LAST_INSERT_ID(), 'This person did not pay his dues.')";


The LAST_INSERT_ID() will return the latest ID generated from an auto_increment column.