onix Posted October 20, 2012 Share Posted October 20, 2012 (edited) Okay, sorry if it's a terribly easy/newb-ish task. But I really need this figured out, so I appreciate any help at all. I have a simple online exam task with just a couple of pages, just started learning PHP. The process is like this: 1.) a new user inputs his information on the first page, it is stored in the database 'dbuser'. 2.) he directed to another page confirming his entry 3.) he takes a simple exam with radio buttons and stuff 4.) after submitting, his score is stored in another database and shown in the next page. My problem is, I wanted to get the primary key (auto-incremented) from the current user just right after he's submitted his information so that I can store it as foreign key in another database (where I would store his score). A rather reckless solution to this was to cross reference his recent information with WHERE to get it, but I believe in the long run it would be a ridiculous thing to to have as it would cause redundancy and inconsistency, and thus not good practice. Would anyone please help me, how do I get the primary key from the user who's just inputted his information in the database? This is my simplified code scenario: Info input page: Information page <form method="post" action="is_db.php"> Name: <input type="text" size=7 name="frm_name"><br><br> Address: <input type="text" size=10 name="frm_address"><br><br> <input type="submit" value="Submit information"> </form> Information input confirmation page: <?php include("dbconnect.php"); // database connect to 'dbuser' ?> $name=$_POST["frm_name"]; $address=$_POST["frm_address"]; $query=" insert into user_info (name, address) values ('$name', '$address')"; mysql_query($query) or die(mysql_error()); User information added! <form method="link" action="questions.php"> <input type="submit" value="Proceed to questions"> </form> Questions page: <form method="post" action="questions_db.php"> 1. What is the meaning of ISP? <br> <input type=radio name='q1' value=0>asdf<br> <input type=radio name='q1' value=0>asdf<br> <input type=radio name='q1' value=0>asfd<br> <input type=radio name='q1' value=1>Internet Service Provider<br> <br> <input type="submit" value="Submit answers"> </form> The next page should be where I should already have the primary key of the examtaker so I can store into to an 'admin' database where I will store his score in the 'scores' table. The next page also shows the score. Thanks for considering and helping, guys! Edited October 20, 2012 by onix Quote Link to comment https://forums.phpfreaks.com/topic/269708-simple-beginner-php-problem-getting-primary-key-from-recent-database-entry/ Share on other sites More sharing options...
JohnTipperton Posted October 20, 2012 Share Posted October 20, 2012 (edited) Does your primary key id is auto increment? if that is the case use mysql_insert_id() or LAST_INSERT_ID(). Edited October 20, 2012 by JohnTipperton Quote Link to comment https://forums.phpfreaks.com/topic/269708-simple-beginner-php-problem-getting-primary-key-from-recent-database-entry/#findComment-1386523 Share on other sites More sharing options...
onix Posted October 20, 2012 Author Share Posted October 20, 2012 Does your primary key id is auto increment? if that is the case use mysql_insert_id(). Yes sir, it is. Quote Link to comment https://forums.phpfreaks.com/topic/269708-simple-beginner-php-problem-getting-primary-key-from-recent-database-entry/#findComment-1386525 Share on other sites More sharing options...
JohnTipperton Posted October 20, 2012 Share Posted October 20, 2012 (edited) Yes sir, it is. use LAST_INSERT_ID() function in mysql it will determine the last inserted value. SELECT LAST_INSERT_ID(); Edited October 20, 2012 by JohnTipperton Quote Link to comment https://forums.phpfreaks.com/topic/269708-simple-beginner-php-problem-getting-primary-key-from-recent-database-entry/#findComment-1386526 Share on other sites More sharing options...
Barand Posted October 20, 2012 Share Posted October 20, 2012 One thing to be aware of. The last_insert_id is valid on a per-connection basis so you need to get its value immediately after inserting the user record - store it in a variable using mysql_insert_id(). Connections are lost when the page script finishes executing so it won't be available on the next page. Quote Link to comment https://forums.phpfreaks.com/topic/269708-simple-beginner-php-problem-getting-primary-key-from-recent-database-entry/#findComment-1386544 Share on other sites More sharing options...
onix Posted November 14, 2012 Author Share Posted November 14, 2012 Thanks a lot for the help, guys! Quote Link to comment https://forums.phpfreaks.com/topic/269708-simple-beginner-php-problem-getting-primary-key-from-recent-database-entry/#findComment-1392217 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.