【发布时间】:2014-09-14 13:58:18
【问题描述】:
我在代码底部附近的 if else 语句有问题。
如果条件为真,它正确地回显登录和用户名“super_user”,但如果条件为假,我得到一个空白屏幕,它不会回显我的失败消息“nope”
“超级用户”和“密码”从上一页的 html 输入表单中传递
我不确定我在这里做错了什么,我真的是 PHP 新手。
<?php
session_start();
if ($_POST['Submit2']) {
$super_user= $_POST['super_user'];
$password= $_POST['password'];
$db = new mysqli('*******', '******', '*******', '******');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
SELECT *
FROM `admin`
WHERE password='$password' AND super_user='$super_user'
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
if (($super_user == $row['super_user'] && $password == $row['password'] )){
echo "logged in ";
echo "<br>";
echo $row['super_user'];
}
else
{
echo "nope";
}
}
}
?>
我正在尝试制作一个简单的登录系统,该系统将设置会话并使用以下内容重定向回主页:
$_SESSION['loggedin'] = $super_user;
header("Location:index.php");
exit;
但在我这样做之前,我只想回显用户名“super_user”,看看我的 if else 语句是否有效。
【问题讨论】:
-
不管错误 - 你需要阅读 SQL 注入 - stackoverflow.com/questions/60174/…
-
你是新手,你有一个空白页。我想这是给你的:How to get useful error messages in PHP ?
-
好点!我会这样做的。
标签: php if-statement