#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Simple Beginner Php Problem, Getting Primary Key From Recent Database Entry.


    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:
    PHP Code:
    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 Code:

    <?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:
    PHP Code:
    <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!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Use the mysql_insert_id function immediately after your insert query, then store it in a session if you need to access it on another page.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by E-Oreo
    Use the mysql_insert_id function immediately after your insert query, then store it in a session if you need to access it on another page.
    Okay, I think I get what you mean.

    I believe I should make this adjustment right after my insert query codes:
    PHP Code:

    <?php

    $getid 
    mysql_insert_id();

    echo 
    "<form method='link' action='questions.php?=".$getid."'>";
    echo 
    "<input type='submit' value='Proceed to questions'>";
    echo 
    "</form>";

    ?>
    .. and then use GET in the next page.

    I tried to look up the suggested function's syntax, but I received some suggestions to use MySQLi because of the deprecation of the original function.
    Are you aware of that, sir?

    Anyway, I appreciate your help!
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    That's close, but you would not want to pass the id in the URL like that - the user would be able to modify it and pretend to be a different user.

    Yes, the MySQL library is deprecated; but the MySQLi and pdo libraries both have equivalent functions.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo