Forums

PHP

This topic is locked

mysql_insert_id - retrieve on "after insert" page

Posted 26 Apr 2004 16:32:19
1
has voted
26 Apr 2004 16:32:19 Dave Desormeaux posted:
A common issue I think, but one that's stumping me...

I've used the mysql_insert_id variable on a page that adds (staff) records to a company page. After uploading I want to go to a page that says Thank you. JOHN DOE has been added to your staff.

The recordset I created tests fine - I can retrieve the staff_name using the server variable. But I can't get it to display on the page.

This is the code:

<?php require_once('../Connections/connNutshell.php'); ?>
<?php


$last_id = mysql_insert_id();
$colname_rsStaff = $HTTP_SERVER_VARS['$last_id'];
if (isset($HTTP_SERVER_VARS['$last_id'])) {
$colname_rsStaff = (get_magic_quotes_gpc()) ? $HTTP_SERVER_VARS['$last_id'] : addslashes($HTTP_SERVER_VARS['$last_id']);
}
mysql_select_db($database_connNutshell, $connNutshell);
$query_rsStaff = sprintf("SELECT * FROM staff WHERE staff_id = %s", $colname_rsStaff);
$rsStaff = mysql_query($query_rsStaff, $connNutshell) or die(mysql_error());
$row_rsStaff = mysql_fetch_assoc($rsStaff);
$totalRows_rsStaff = mysql_num_rows($rsStaff);

// set the page title and include the html header
$page_title = 'Nutshell Music';
include ('./header_admin.php');
?>
<p></p>
<p> <?php echo $row_rsStaff['Staff_firstname']; ?> has been added</p>
<p></p>
<p>
<?php
// set the page footer and close db
include ('../footer.php');

mysql_free_result($rsStaff);
?>
</p>

Thanks
Dave
PHP newbie

Replies

Replied 26 Apr 2004 18:27:13
26 Apr 2004 18:27:13 Dale Stevenson replied:
You could try adding this line, assuming the connection was created using DW(MX)

<pre id=code><font face=courier size=2 id=code>
mysql_select_db($database_connNutshell, $connNutshell);
</font id=code></pre id=code>

just before

<pre id=code><font face=courier size=2 id=code>
$last_id = mysql_insert_id();
</font id=code></pre id=code>
Replied 26 Apr 2004 18:56:44
26 Apr 2004 18:56:44 Dave Desormeaux replied:
Thanks but that doesn't seem to do it.

I've found that if I leave the default code that DWMX creates, I get the line:

$colname_rsStaff = "1"

which of course, returns the first record.
I find this odd, since I've created the recordset so that it filters staff_id by server variable $last_id !!!

If I replace "1" with "$last_id", I get an error message "undefined variable", which tells me that somehow I have to define a variable that will pull the new ID from the database.

I'd be happy to insert hard code that will give $colname_rsStaff the required info, I just don't know how to do that.

Dave
Replied 26 Apr 2004 20:29:30
26 Apr 2004 20:29:30 Dave Desormeaux replied:
Thanks but that doesn't seem to do it.

I've found that if I leave the default code that DWMX creates, I get the line:

$colname_rsStaff = "1"

which of course, returns the first record.
I find this odd, since I've created the recordset so that it filters staff_id by server variable $last_id !!!

If I replace "1" with "$last_id", I get an error message "undefined variable", which tells me that somehow I have to define a variable that will pull the new ID from the database.

I'd be happy to insert hard code that will give $colname_rsStaff the required info, I just don't know how to do that.

Dave
Replied 04 May 2004 15:33:39
04 May 2004 15:33:39 Dale Stevenson replied:
You could try changing this

$last_id = mysql_insert_id();
$colname_rsStaff = $HTTP_SERVER_VARS['$last_id'];

to this

$colname_rsStaff = mysql_insert_id();
Replied 04 May 2004 15:34:56
04 May 2004 15:34:56 Dale Stevenson replied:
ah i think ive seen the problem

$last_id = mysql_insert_id();
$colname_rsStaff = $HTTP_SERVER_VARS['<b>$</b>last_id'];

i think this $ symbol is not needed here

Reply to this topic