donglu9898 2017-02-15 15:50
浏览 25
已采纳

登录时尝试设置会话数据

I am trying to set some session data when my users login, so I can use the users Id and also other pieces of data:

the issue I find is now im using prepared statements if I select anything more and then the username in the select statement it fails.

$stmt = $conn->prepare("SELECT username, password FROM users WHERE username = ?");
$stmt->bind_param('s', $username);

If I include say id or email in the select it breaks.

I am also trying to get the user data form the database but if I try to bind data from the table to a session variable it dosn't pass it through.

if ($row = $stmt->fetch()) {

    // Check if password matches
    if (password_verify($_POST['upassword'], $password)) {

        $_SESSION['username'] = $row['username'];
        header('Location: ../index.php');

    } else{
        echo "Password is wrong!";
    }

} else{
    echo "Wrong username";
}

Im new to this way of coding so please be patient. I have started sessions in both this file and the destination file.

Error when adding more columns to select in the statment:

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in...

UPDATE

I am looking to retrieve the users details and set them to session variables so I can use them across the site once the user is logged in (providing the login is successful):

$stmt = $conn->prepare("SELECT id, email, username, password FROM users WHERE username = ?");
$stmt->bind_param('s', $username);

$username = $_POST['ulogin'];
$password = $_POST['upassword'];

$stmt->execute();
$stmt->bind_result($id, $email, $username, $password);

// Fetch & check user details
if ($row = $stmt->fetch()) {

    // Check if password matches
    if (password_verify($_POST['upassword'], $password)) {

        // Set the session data here by getting the username, email, id etc..

        header('Location: ../index.php');

    } else{
        echo "Password is wrong!";
    }

} else{
    echo "Wrong username";
}

$stmt->close();
$conn->close();
  • 写回答

1条回答 默认 最新

  • dqde43215 2017-02-15 16:09
    关注

    Read the manual on bind_result():

    "When mysqli_stmt_fetch() is called to fetch data, the MySQL client/server protocol places the data for the bound columns into the specified variables var1, ...."

    "Note that all columns must be bound after mysqli_stmt_execute() and prior to calling mysqli_stmt_fetch(). Depending on column types bound variables can silently change to the corresponding PHP type."

    As I said in comments:

    "nowhere in your code did you include bind_result(). I'm suspecting not enough or too many variables passed in it which doesn't match your SELECT'ed columns. Take this as a temporary "answer" to this question."

    So, if you added or reduced the amount of variables in that function and it didn't match the number of columns in SELECT, then that's the problem.

    Btw, you should add an exit; after your header. Otherwise, your code may want to continue to execute further down below.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数