简体   繁体   English

无法使用SET更新SQL表值

[英]Cannot update SQL table value using SET

I have tried many different forms of syntax, and no matter what I use, I cannot get the boolean logged_in in my SQL database to change. 我尝试了许多不同形式的语法,无论使用什么语法,都无法更改SQL数据库中的布尔值logging_in

login.php login.php

<?php
session_start();
?>
<html>
<body>
    <h1>User Login Form - PHP MySQL Login System</h1>
    <?php
    if (!isset($_POST['submit'])){
    ?>
    <!-- The HTML login form -->
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        Username: <input type="text" name="username" /><br />
        Password: <input type="password" name="password" /><br />
        <input type="submit" name="submit" value="Login" />
    </form>

    <?php
require_once("settings.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}

$username = $_POST['username'];
$password = sha1($_POST['password']);

$sql = "SELECT * from Users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
$result = $mysqli->query($sql);
if ($result->num_rows == 1) {

#update the logged_in value here
$sql2 = "UPDATE Users SET logged_in = 1 WHERE username = '$username'";

$location = "contacts.php";
header("Location: $location");
} else {
$location = "login.php";
header("Location: $location");
}
}
?>
</body>
</html>

I successfully log in using my settings.php : 我使用我的settings.php成功登录:

<?php
    # mysql db constants DB_HOST, DB_USER, DB_PASS, DB_NAME
    const DB_HOST = 'localhost';
    const DB_USER = 'Timmy';
    const DB_PASS = 'mypassword';
    const DB_NAME = 'IT210';
?>

but the logged_in value remains the same: login_in值保持不变: 在此处输入图片说明

$sql2 = "UPDATE Users SET logged_in = 1 WHERE username = '{$username}'";

Try this 尝试这个

#update the logged_in value here
$sql2 = "UPDATE Users SET logged_in = 1 WHERE username = '$username'";
$mysqli->query($sql2);    

You are missing Execute update query. 您缺少执行更新查询。 ( $mysqli->query($sql2);) ($ mysqli-> query($ sql2);)

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

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