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!

Return the Primary key to the page you want to, after submitting the score.

if(submittedScore)
{
  return primaryKey;
}  

after the submit button is clicked the primary key should be returned back to the page you want to.

Member Avatar for LastMitch

@onixbwcrap

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).

Since you started learning mysql, my advice learn mysqli because most of the mysql function are DEPRECATED.

You need to used this mysql_insert_id to get the primary key (auto-incremented) from the user after submitted.

Here is a link of examples:

http://php.net/manual/en/function.mysql-insert-id.php

If you decide to used mysqli then you can used this mysqli::$insert_id to get the primary key (auto-incremented) from the user after submitted.

Here is a link of examples:

http://www.php.net/manual/en/mysqli.insert-id.php

commented: To Rectify what some retard did to LastMitch +0

I will not tire to point out that PHP has a really great OOP interface for accessing databases named PDO. Every PDO object has the method lastinsertid witch returns the last inserted id (of the auto increment primary key)

Moreover lastinsertid method works great with MySql databases and I would recommend more over to use it (like any statement) with prepared statements.

@see http://www.php.net/manual/en/pdo.lastinsertid.php

That's for the help, guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.