2017-02-15 88 views
0

我嘗試了許多不同形式的語法,無論我使用什麼,我都無法在我的SQL數據庫中獲取布爾值logged_in以進行更改。無法使用SET更新SQL表值

的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> 

我成功登錄使用我的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'; 
?> 

LOGGED_IN值保持不變: enter image description here

+0

替換$ result = $ mysqli-> query($ sql);用$ result = $ mysqli-> query($ mysqli,$ sql); – Gulshan

+0

您的數據未提交。或者你使用自動提交? – Massimo

+0

@velkoon和更新$ sql2 =「更新用戶SET logged_in = 1 WHERE用戶名='$用戶名'」;用$ sql2 = $ mysqli-> query($ mysqli,「UPDATE Users SET logged_in = 1 WHERE username ='$ username'」); – Gulshan

回答

-2

試試這個

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

你缺少執行更新查詢。 ($ mysqli-> query($ sql2);)

+0

非常感謝!我今天沒有情人節,所以是我的嗎? :) – velkoon

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