Forum Moderators: coopster

Message Too Old, No Replies

mysql_insert_id won't work

         

Ann_G

4:33 pm on Sep 14, 2004 (gmt 0)

10+ Year Member


I have a DB with two tables that collect data from a form. My main problem is that I can't get the auto- incremented id returned and I need to put it into the 2nd table in order to join the tables. The data goes into the MYSQL tables and generates a number in the primary key field (just like I want but I need that number to come back and to go into the next table as a foreign key).

Also I tried the regular way that all the books suggest of INSERTING INTO a table and that never worked. Then I came across a post (forum88/322) in this forum that tipped me off to using the ('".mysql_escape_string($_REQUEST['value'])."')"; and that worked great for getting the data into the tables. That's when I decided to join this forum.

If anyone can help I would really appreciate it. My script looks like this:

//Add the incident to the incidents table.
$query = "INSERT INTO incidents (filed_by, first_name, last_name)
VALUES ('".mysql_escape_string($_REQUEST['filed_by'])."',
'".mysql_escape_string($_REQUEST['first_name'])."',
'".mysql_escape_string($_REQUEST['last_name'])."')";

if(!mysql_query($query) ) {
print "Error with query: \"$query\"<br />".mysql_error();
// Simple error handling...
}

$incident_id = mysql_insert_id()

$query = "INSERT INTO police (call_made, call_time, incident_id) VALUES (
'".mysql_escape_string($_REQUEST['call_made'])."', '".mysql_escape_string($_REQUEST['call_time'])."' , '".mysql_escape_string($_REQUEST['incident_id'])."')";

if(!mysql_query($query) ) {
print "Error with query: \"$query\"<br />".mysql_error();
// Simple error handling...
}

DooinMyHeadIn

12:02 am on Sep 15, 2004 (gmt 0)

10+ Year Member



Hi Ann
From the code you poted I am assuming you wish to insert the last insert ID to the police table. If so the the value you are trying to place doesnt exist ($_REQUEST['incident_id']). You created the variable above as $incident_id = mysql_insert_id() so look at the code below and note the change. If this is your aim the code below should work. If not post again with the table structures and I or someone else can look further.

//Add the incident to the incidents table.
$query = "INSERT INTO incidents (filed_by, first_name, last_name)
VALUES ('".mysql_escape_string($_REQUEST['filed_by'])."',
'".mysql_escape_string($_REQUEST['first_name'])."',
'".mysql_escape_string($_REQUEST['last_name'])."')";

if(!mysql_query($query) ) {
print "Error with query: \"$query\"<br />".mysql_error();
// Simple error handling...
}

$incident_id = mysql_insert_id()

$query = "INSERT INTO police (call_made, call_time, incident_id) VALUES (
'".mysql_escape_string($_REQUEST['call_made'])."', '".mysql_escape_string($_REQUEST['call_time'])."' , '".mysql_escape_string($incident_id)."')";

if(!mysql_query($query) ) {
print "Error with query: \"$query\"<br />".mysql_error();
// Simple error handling...
}