How to parse a value when using header( ) to redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

How to parse a value when using header( ) to redirect

Post by mcccy005 »

When a user submits a form, the submit function calls the current webpage.
If all of the fields in the form are filled in correctly, the data (using $_POST, $_GET etc) is inserted into a mysql database. Thing with this is that theres a few webpages which the user has to enter data into which relate to the same record within the mysql database.


1. Thus, when the data is inserted into the database from the FIRST webpage, is there any methods I can use which will return the id_number of the record which I have just created?? (As in the id number you put in yourself into mysql using auto_increment, primary_key etc).
I know if I can't do this, I can query the database with all the values I just entered into it, but the above would be much simpler and be 100% accurate rather than 99.99% accurate.

2. When I get this id_number from the database; I want to parse it on to the next webpage which is called using the header("location:next_page.php") function. THat way, when the user continues to fill out data required for the same record in the next webpage, I can "INSERT where id_number=<<id_number parsed from previous webpage>>".

I'm sure theres something simple and well known out there to do this (perhaps SOMEHOW using hidden html fields??) but I just have no idea what to even search for!!
Thanks.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

1) mysql_insert_id()

2)

Code: Select all

$id = mysql_insert_id()

header("Location: next_page.php.php?id=$id"):
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

geez thanks for the fast reply.
I assume I access the id variable using $_GET('id')??

Also, regarding that mysql_insert_id; is there any possibility that in the split second between inserting the data and calling the mentioned function that ANOTHER user inserts data into the database thus meaning I'll have the incorrect id_number??/

Thanks again.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

mcccy005 wrote:I assume I access the id variable using $_GET('id')??
Yes
mcccy005 wrote:Also, regarding that mysql_insert_id; is there any possibility that in the split second between inserting the data and calling the mentioned function that ANOTHER user inserts data into the database thus meaning I'll have the incorrect id_number??
The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions."
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Pimptastic wrote:
mcccy005 wrote: I assume I access the id variable using $_GET('id')??
Yes

Code: Select all

$_GET['id']
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

you could also put the id into $_SESSION[] 8O but I'd recommend the method mentioned by others
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

I seem to get an error when using mysql_insert_id( ) - something about unknown method or whatever (sorry - don't have the error available on hand at the moment).
Is there something in php.ini I need to configure or something I need to install in order to get this function to work???
Thanks
Post Reply