简体   繁体   English

通过从MySQL获取id将$ _SESSION ['name']更改为$ _SESSION ['id]

[英]change $_SESSION['name'] to $_SESSION['id] by fetching id from MySQL

I have a log in form, that starts with session email. 我有一个登录表单,以会话电子邮件开头。 Now since I want to be able to update the email address by a form, without broke the session I need to change the session to the id. 现在,因为我希望能够通过表单更新电子邮件地址,而不会中断会话,我需要将会话更改为id。 The problem is that it wont send me the id value to the session, it only gives me value 1, like this: www.mysite.com/profile.php?uid=1 instead of returning the real id number. 问题是它不会向我发送会话的id值,它只给我值1,如下所示:www.mysite.com/profile.php?uid = 1而不是返回真实的id号。

Any ideas? 有任何想法吗?

<?php
if(isset($_SESSION['email'])){
    $_SESSION['uid'] = fetch_user_id($_SESSION['email']);
    header("Location: profile.php?uid=" . $_SESSION['uid']);
    die();
}
?> 

<?php 
function fetch_user_id($email){
    global $db;
    $query = $db->query("SELECT id FROM user WHERE email='{$_SESSION['email']}'");
    $row = $query->fetch(PDO::FETCH_ASSOC);
    return true;
} 
?>

$row是存储结果ie(数组)找到的结果,你想要的是$row['id']

That is because you are returning the value true from your fetch_user_id function. 那是因为你从fetch_user_id函数返回值true In order to get the correct results you need to return the users id from the $row array. 为了获得正确的结果,您需要从$row数组返回用户ID。

function fetch_user_id($email) {
    global $db;
    $query = $db->query("SELECT id FROM user WHERE email='{$_SESSION['email']}'");
    $row = $query->fetch(PDO::FETCH_ASSOC);
    return $row['uid']; // whatever your user id column is called
}

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

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