View Full Version : Can't retrieve ID of row just posted

07-03-2010, 05:29 PM
I'm having a similiar problem to this thread (http://codingforums.com/showthread.php?t=199362) but not exactly as i can't retrieve the AI generated ID of the row i just posted to.

I have two pages (HTML):

<form method="post" class="uniForm" action="global/guest.php">

<fieldset class="inlineLabels">

<div class="ctrlHolder">
<label for="FullName"><em>*</em>Full Name</label>
<input name="FullName" id="FullName" value="" size="35" maxlength="30" type="text" class="textInput" />
<p class="formHint">E.g.: Jonathan Doe</p>

<div class="buttonHolder">
<button type="submit" name="submit" value="submit" class="primaryAction">Submit</button>



...and thats repeated a couple times but etc. etc. w/e

Page two, where the results from the form are being posted and inserted into the db and than displayed


$FullName = $_POST["FullName"];

mysql_connect ("localhost", "user", "pass") or die ('Could not connect');
mysql_select_db ("dbname");

$query="INSERT INTO guest

(ID, FullName)


('NULL', '".$FullName."')";

mysql_query($query) or die ('data was not submitted to db');

echo "this is the ID: ". $ID."

<tr><td><b>Full Name:</b></td><td> ". $FullName." </td></tr>


but the id does not appear where i have the echo to show the id

can this not be done on the same page as the post and insert?

07-03-2010, 05:36 PM

07-03-2010, 05:49 PM
You don't automatically get an ID back after making an insert query. Specially not into a predefined variable. Think about it, if someone was using the $ID variable before making a query, they'd then lose the value of whatever's in it. Just use the php function mysql_insert_id or more generically, the mysql function LAST_INSERT_ID to retrieve the last inserted id.

07-03-2010, 10:14 PM
thanks guys, i figured it would be something of that nature, but wasn't familiar with it