duande1146 2016-04-13 19:08
浏览 13
已采纳

用户登录时更新mysql数据库中的列

I'm using this code to login user and I want to update the value in column loggedin to yes in mysql database. I tried to update it before sending header but it doesn't get updated. Where should I put the code to update the column?

if (isset($_POST['login']))
    {
    $username = trim(mysqli_real_escape_string($con, $_POST['username']));
    $password = trim(mysqli_real_escape_string($con, $_POST['password']));
    $md5password = md5($password);

    // check user and password match to the database

    $query = mysqli_query($con, "SELECT * FROM `user` WHERE username='$username' AND password='$md5password'");

    // check how much rows return

    if (mysqli_num_rows($query) == 1)
        {

        // login the user
        // get the id of the user

        $fetch = mysqli_fetch_assoc($query);

        // start the session and store user id in the session

        session_start();
        $_SESSION['id'] = $fetch['id'];
        $_SESSION['username'] = $fetch['username'];
        $query = mysqli_query($con,"UPDATE user SET loggedin = 'yes' WHERE userid = 1;");
        header("Location: message.php");
        }
      else
        {

        // show error message

        echo "<div class='alert alert-danger'>Invalid username Or password.</div>";
        }
    }
  • 写回答

2条回答 默认 最新

  • dongliqin6939 2016-04-13 19:25
    关注

    You're not updating the correct userid. You're updating userid = 1 instead of the ID belonging to the user who logged in. It should be:

    $query = mysqli_query($con,"UPDATE user SET loggedin = 'yes' WHERE id = {$_SESSION['id']};");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料