dtziv24262 2016-05-29 12:56
浏览 45
已采纳

在PHP登录表单代码中跳过一个IF块

I'm a beginning level PHP programmer. I've created a student login system to display exam marks for my assignment purpose. I have a MySQL DB to store student and marks details. I created two IF blocks to validate empty input fields and wrong credentials. first one is working fine but, another one is not executed. it means, results page is displayed without any details when I enter wrong username and password. Could anyone please help me to find my mistake?

<?php
require '../scripts/database_connection.php';

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

if (($username != "") && ($password != "")){
$login_query = "SELECT * FROM student WHERE username= " . "'$username' AND password= " . "'$password'"; 

} else {
die("<p>Username and password cannot be empty</p>");
}  

$result = mysql_query($login_query);

if (!$result){
die("<p>Password and username is not correct" . mysql_error() . "</p>");

} else {
$studentrow = mysql_fetch_array($result);
$index = $studentrow['index_no'];
}

?>

<html>
<head>
<title>Results form</title>
<link href="passpaper2014.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Final Results</h2>
<div id="tableContainer">
<p>Name: <?php echo $studentrow['name'];?></p>
<p>Index No: <?php echo $studentrow['index_no']; ?></p>
<table class="table" border="1">
<col width="250">
<col width="80">
<tr>
<th>Subject</th>
<th>Marks</th>
</tr>
<?php 
$retrive_query = "SELECT subject,marks FROM mark WHERE index_no= " . "'$index'";
$marksresult = mysql_query($retrive_query);
while ($row = mysql_fetch_array($marksresult)){

echo "
<tr>
<td>". $row['subject'] . "</td>
<td>" . $row['marks'] . "</td>
</tr>";
}

?>
</table>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douyan6742 2016-05-29 13:34
    关注
    $result = mysql_query($login_query);
    if (!$result){
        die("<p>Password and username is not correct" . mysql_error() . "</p>");
    }
    

    Here is the problem. Even if your query doesn't return any value, $result will contain a query resource, so your if (!$result) is invalid.

    This should work:

    $result = mysql_query($login_query);
    $studentrow = mysql_fetch_array($result);
    if (!$studentrow){
        die("<p>Password and username is not correct" . mysql_error() . "</p>");
    } else {
        $index = $studentrow['index_no'];
    }
    

    As stated in the comments, your code is really vulnerable to SQL injection. Check it and fix before going online with a vulnerable website

    This question can help you: How can I prevent SQL injection in PHP?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)