Jump to content

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


onix

Recommended Posts

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!

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.

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.