user1084949
user1084949

Reputation: 65

php pass auto incremnt id in next page

I have a form where I insert a record and the ID (Primary key is Auto increment). How I can show the ID in the next page after submit. I tried to echo mysql_insert_id(); in next page but didnt work:

<?php echo "Your refrence number is = ". mysql_insert_id(); ?>

Upvotes: 0

Views: 1153

Answers (3)

Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

Friend, Let's see your issue this way. You have two pages A, and B. You insert data to the database in page A and get the last insert id with mysql_insert_id(); Now the issue is you want to use this in page B. I can propose you 4 ways. but I will explain you session base way since I think it is the best solution for you.

  1. POST
  2. GET
  3. Cookie
  4. Session

Ok In your page A top of your page you write session_start(); remember it should be the first line. Once you get the last insert ID in page a you create a session variable.

$_SESSION["lastID"] = mysql_insert_id();

And in page B also you initiate the session as you did in page A. Then you can simply get the last ID using.

$lastID =  $_SESSION["lastID"];

Hope you understand.

Upvotes: 2

xdazz
xdazz

Reputation: 160833

You should get the last insert id in the the page where you insert the record, and then pass the the id to the next page (by url parameter).

Upvotes: 0

user1204739
user1204739

Reputation:

Use PHP Session variable. Put mysql_insert_id() in session after insert and access on next page.

$_SESSION["yourID"] = mysql_insert_id();

and access it on next page.

Upvotes: 1

Related Questions