开发者

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 se开发者_JAVA百科nt 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 ?


You will want to call this function after the insert:

http://php.net/mysql_insert_id


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']; 


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜