Jump to content

Can INSERTS return Primary Key values!


pthurmond

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.