simon551 Posted April 25, 2007 Share Posted April 25, 2007 Hi, Newbie here. Just wondering if anyone can help me/point me to where I can find info on how to retrieve the primary key from a table after submitting a record through a form. I want to pull the primary key out, drop it on another form on the next page. Is that a stupid idea; like I said, newbie... thanks in advance! Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/ Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 $var = mysql_insert_id(); gets id of last inserted record in current mysql connetion Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/#findComment-237892 Share on other sites More sharing options...
simon551 Posted April 25, 2007 Author Share Posted April 25, 2007 awesome! thanks, that will be a good start. another dumb question: how would I read that variable? Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/#findComment-237900 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 echo $var; will print it to the screen Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/#findComment-237902 Share on other sites More sharing options...
simon551 Posted April 25, 2007 Author Share Posted April 25, 2007 Parse error: syntax error, unexpected T_STRING in C:\Program Files\xampp\htdocs\vtmorg\Timesheets\whatever.php on line 9 <?php require_once('../Connections/conn_org.php'); ?> <?php mysql_select_db($database_conn_org, $conn_org); $query_rswhatever = "SELECT * FROM timesheet_entries"; $rswhatever = mysql_query($query_rswhatever, $conn_org) or die(mysql_error()); $row_rswhatever = mysql_fetch_assoc($rswhatever); $totalRows_rswhatever = mysql_num_rows($rswhatever); $var = mysql_insert_id(); if ((any form post != "")) { setcookie("whatever", "$var", time()+(60*60*24*30), "/", "", 0); } echo var$ Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/#findComment-237922 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 i thought you were inserting a row into the table... then you would use mysql_insert_id() you're not actually inserting anything Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/#findComment-237924 Share on other sites More sharing options...
simon551 Posted April 25, 2007 Author Share Posted April 25, 2007 It worked! Thank you so much, Benjamin! if (isset($_POST["insert"])) $var = mysql_insert_id(); { setcookie("whatever", "$var", time()+(60*60*24*30), "/", "", 0); } Link to comment https://forums.phpfreaks.com/topic/48585-solved-retrieve-the-primary-key-after-submitting/#findComment-237936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.