简体   繁体   English

PHP和MySql ID

[英]PHP and MySql ID's

Basically I have setup up SESSIONS on my site. 基本上,我已经在自己的网站上设置了SESSIONS。

Once a user registers an Auto Incremented ID is made for this user (I hope that is the right word) 用户注册后,将为此用户创建一个自动递增的ID(我希望这是正确的词)

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 我想做的是一旦发送POST方法并且在下一页上注册了用户,请说操作是否为Welcome.php,我想在Welcome.php页面上回显用户ID

Is this possible ? 这可能吗 ?

If so how ? 如果可以,怎么办?

You will want to call this function after the insert: 您将要在插入后调用此函数:

http://php.net/mysql_insert_id 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(); 在两个页面上添加session_start(); before you output any code. 在输出任何代码之前。

You can then store and read variables in the $_SESSION superglobal: 然后,您可以在$_SESSION超全局变量中存储和读取变量:

// 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. 首先,我不知道为什么每次注册后都会增加用户ID。 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. 只需运行一个SQL查询即可为您获取该值。

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). 如果您正在使用会话,则必须在$_SESSION存储一些标识符来实际分辨出该用户(例如电子邮件,ID等)。 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. 我认为mysql_insert_id()是您所需要的。 Basically, you should call this function right after you insert to get the latest added id. 基本上,您应该在插入后立即调用此函数以获得最新添加的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM