Oliver Bayes-Shelton
Oliver Bayes-Shelton

Reputation: 6292

PHP and MySql ID's

Basically I have setup up SESSIONS on my site.

Once a user registers an Auto Incremented ID is made for this user (I hope that is the right word)

What I want to do is once the POST method is sent and the user is signed up on the next page say if the action is Welcome.php I would like on the Welcome.php page to echo the users ID

Is this possible ?

If so how ?

Upvotes: 0

Views: 6894

Answers (4)

Greg
Greg

Reputation: 321638

It sounds like you want a basic introduction to sessions. Everything you need should be in that link but as a very basic guide:

On both pages add session_start(); before you output any code.

You can then store and read variables in the $_SESSION superglobal:

// Set this when the user registers
$_SESSION['userId'] = mysql_insert_id(); 

// This will work on any page now, after the user logs in
echo $_SESSION['userId']; 

Upvotes: 2

NawaMan
NawaMan

Reputation: 25687

I think mysql_insert_id() is what you need. Basically, you should call this function right after you insert to get the latest added id.

Upvotes: 0

RaYell
RaYell

Reputation: 70414

First of all I don't know why you increment user ID after every sign up. Seems awkward for me. I assume this value is stored in the database. Just run a SQL query that will fetch this value for you.

If you are using sessions then you have to have some identifier stored in $_SESSION to actually tell which user is that (like email, ID, whatever). Use this value to query the db. It's hard to give the exact solution without knowing the table schema but something like this should do the trick (I'm assuming that you store user email in the session to identify it)

$q = "SELECT id FROM `users` WHERE `email` = '{$_SESSION['email']}'";
$r = mysql_query($q);
$res = mysql_fetch_num($r);
$userId = $res[0];

Upvotes: 0

Mobs
Mobs

Reputation: 1562

You will want to call this function after the insert:

http://php.net/mysql_insert_id

Upvotes: 3

Related Questions