[英]php mysql - add user_id to session variable
When I register a user I add them to a database and they get an id which is auto generated by auto_increment. 当我注册用户时,我将他们添加到数据库中,并且他们获得一个由auto_increment自动生成的ID。 Now I'm trying to add that id to a session variable so I can use it on other pages in the website and manipulate their row in the database. 现在,我正在尝试将该ID添加到会话变量中,以便可以在网站的其他页面上使用该ID并处理它们在数据库中的行。 This is what I have so far. 到目前为止,这就是我所拥有的。
This line adds the form inputs to the db when the user registers: 用户注册时,此行将表单输入添加到数据库:
$insert = mysql_query("INSERT INTO users (`email`, `password`) VALUES ('example','values'))");
Then I'm trying to get the user_id into a variable that I can use on other pages of the website. 然后,我试图将user_id放入一个可以在网站其他页面上使用的变量。 I'm trying this: 我正在尝试:
if ($insert) {
$select_id = mysql_query("SELECT `id` from `users` WHERE `email` = '$form_input'");
$id_arr = mysql_fetch_array($select_id);
$id_var = $id_arr['id'];
session_start();
$_SESSION["Register"] = $id_var[0];
}
But it doesn't work so I'm trying a var_export to see what's in $id_var and I get a value of '3' which doesn't match the user_id that supposed to be stored in the variable. 但是它不起作用,所以我尝试使用var_export来查看$ id_var中的内容,并且得到的值'3'与应该存储在变量中的user_id不匹配。
You need to iterate over the result set to get the value of id
您需要遍历结果集以获得id
的值
$id="";
while ($row = mysql_fetch_array($select_id))
{
$id= $row ['id'];
}
if($id!=""){
session_start();
$_SESSION["Register"] = $id;
}
Note: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 注意:mysql_query在PHP 5.5.0中已弃用,在PHP 7.0.0中已删除。 Instead, the MySQLi or PDO_MySQL extension should be used.Alternatives to this function include: mysqli_query() or PDO::query()
而是应使用MySQLi或PDO_MySQL扩展名。此函数的替代方法包括: mysqli_query() or PDO::query()
Moreover, passing the values directly into the query is not safe. 而且,将值直接传递到查询中是不安全的。 Please, read about SQL injection
请阅读有关SQL injection
You can use mysql_insert_id()
您可以使用mysql_insert_id()
mysql_query("INSERT INTO mytable (1, 2, 3, 'blah')");
$last_id = mysql_insert_id();
See this : mysql_insert_id() 看到这个: mysql_insert_id()
Note: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 注意: mysql_query在PHP 5.5.0中已弃用,在PHP 7.0.0中已删除。 Instead, the MySQLi or PDO_MySQL extension should be used.Alternatives to this function include: mysqli_query() or PDO::query() 而是应使用MySQLi或PDO_MySQL扩展名。此函数的替代方法包括: mysqli_query()或PDO :: query()
as Comments by Jens
. 作为Jens
评论。 I strongly recommend to use mysqli
or even better PDO
. 我强烈建议使用mysqli
甚至更好的PDO
。 As mysql
is depreciated
and completely removed in PHP7
. 由于mysql
已depreciated
并在PHP7
完全删除。
now as per your code try this 现在根据您的代码尝试
if ($insert) {
$select_id = mysql_query("SELECT `id` from `users` WHERE `email` = '$form_input'") or die(mysql_error());// to see error
if($select_id != null){
$id_arr = mysql_fetch_array($select_id);
$id_var = $id_arr['id'];
session_start();
$_SESSION["Register"] = $id_var;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.