oolongdavies Posted May 18, 2007 Share Posted May 18, 2007 To keep this simple, just assume I have 2 forms: Form 1 has two fields, "surname" and "forename". Form 2 has two fields, "address" and "zip code". I have a database with a table called tbl_details, the fields are: user_id - this is an autoincremented value surname forename address zip code When form1 is submitted, the surname and forename are inserted into the table (and a new autonumber is generated in the user_id field) and Form2.php is displayed. Is there any way of retrieving the user_id value of the data recently entered? Thanks in advance O Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/ Share on other sites More sharing options...
hitman6003 Posted May 18, 2007 Share Posted May 18, 2007 http://www.php.net/mysql_insert_id Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/#findComment-256654 Share on other sites More sharing options...
oolongdavies Posted May 18, 2007 Author Share Posted May 18, 2007 Thanks, to be honest, I don't understand. I have tried it but I can't get it to work? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/#findComment-256678 Share on other sites More sharing options...
hitman6003 Posted May 18, 2007 Share Posted May 18, 2007 What code are you using? a basic example: mysql_query("INSERT INTO table_name (col1) VALUES ('foo')"); echo "Foo was inserted with an ID of " . mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/#findComment-256682 Share on other sites More sharing options...
oolongdavies Posted May 18, 2007 Author Share Posted May 18, 2007 Here's the code from form1.php that adds the values to the database and redirects to form2.php if ($_POST['add'] == 'Add Record') { $sql = 'INSERT INTO tbl_details (surname, forename) VALUES ("' .$_POST['surname'].'" , "'. $_POST['forename'] .'")'; mysql_query($sql) or die(mysql_error()); header('location: form2.php'); } How can I retrieve the value of user_id from the record I have just created? Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/#findComment-256694 Share on other sites More sharing options...
hitman6003 Posted May 18, 2007 Share Posted May 18, 2007 mysql_query($sql) or die(mysql_error()); echo "User's ID: " . mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/#findComment-256698 Share on other sites More sharing options...
oolongdavies Posted May 18, 2007 Author Share Posted May 18, 2007 I tried your code on the first form and second forms but it displays 0. Quote Link to comment https://forums.phpfreaks.com/topic/52061-retriveing-autonumber-values/#findComment-256708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.