pthurmond Posted November 22, 2006 Share Posted November 22, 2006 Ok so this sounds pretty dumb, and I am pretty sure that the answer is no, but can an insert query be modified to return the primary key number of the newly inserted row. I am having trouble with my following select script not returning the id number.Here is my script:[code]$query = "INSERT INTO Events (Name, Description, Date, Time, Center_ID) VALUES ('$name', '$desc', '$date', '$time', '$location')";$result = mysql_query($query) or die('Query failed: ' . mysql_error());if($result){ echo 'Event inserted. Finding ID.<br>'; $query = "SELECT Event_ID FROM Events WHERE Name = '$name' AND Description = '$desc' AND Date = '$date' AND Time = '$time' AND Center_ID = '$location' "; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); if($result) { echo 'ID found. Inserting Activities.<br>'; $row = mysql_fetch_array($result, MYSQL_NUM); $eid = $row[0]; foreach($Activities AS $value) { $query = "INSERT INTO Activity_List (Activity_ID, Event_ID) VALUES ('$value', '$eid')"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); if(!($result)) { $error = TRUE; $message .= 'Unable to insert Activity ID: ' . $value . '. Please try again!<br>'; } } echo 'Activities inserted.<br>'; } else { $error = TRUE; $message .= 'Unable to retrieve inserted Event_ID!<br>'; }}else{ $error = TRUE; $message .= '<p>Unable to create a new event. Please go back and try again.</p>';}[/code]Any ideas? It is inserting Event_ID = 0 each time.-Patrick Link to comment https://forums.phpfreaks.com/topic/28162-can-inserts-return-primary-key-values/ Share on other sites More sharing options...
trq Posted November 22, 2006 Share Posted November 22, 2006 [url=http://php.net/mysql_insert_id]mysql_insert_id[/url] might help you. Link to comment https://forums.phpfreaks.com/topic/28162-can-inserts-return-primary-key-values/#findComment-128821 Share on other sites More sharing options...
pthurmond Posted November 22, 2006 Author Share Posted November 22, 2006 Yep, that did it. Link to comment https://forums.phpfreaks.com/topic/28162-can-inserts-return-primary-key-values/#findComment-128865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.