gpt4 book ai didi

php - sql查询每次都失败

转载 作者:行者123 更新时间:2023-11-29 04:21:20 25 4
gpt4 key购买 nike

<html>
<head>
</head>
<body>
<form action="login_process.php" method="post">
SID: <input type="text" name="sid">
<br />
Password: <input type="text" name="pw">
<br />
<input type="submit" value="Login">
</form>
</body>
</html>

login_process.php 文件 enter image description here

<html>
<head>
</head>
<body>
<?php
include ("connection.php");

$sid = $_POST['sid'];
$pw = $_POST['pw'];

setcookie("username", "$sid". time()+86400);

$result = mysqli_query($con, "SELECT SID FROM student_table WHERE SID='$sid' and password='$pw'") or die('Query failed');


if(!$result){
echo "Failed";
} else {
echo "success". $_COOKIE['username'];
$_SESSION['username'] = $sid;
}
?>
</body>
</html>

我的student_table 中有数据。但无论我输入什么,它都会说成功。即使我在没有任何输入的情况下单击登录按钮,它仍然显示成功。请帮忙。

最佳答案

在 sql 查询中赋值时应该使用引号。

SELECT 
SID
FROM
student_table
WHERE
SID = '$sid' and password = '$pw'

也期待准备好的语句来保护自己免受 sql 注入(inject)。您还应该添加下面的代码来获取选定的行:

while ($row = mysqli_fetch_array($result)) {
$_SESSION['username'] = $row['SID'];
}

关于php - sql查询每次都失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com