donglingsai2880 2015-06-19 22:50
浏览 35
已采纳

我有一个连接到MySQL数据库的php登录脚本,我的else语句没有运行[重复]

This question already has an answer here:

Here is my back end for the login page it first checks the username against the database if the user exists if checks the password against the user in the data base if they match we log in if they don't we don't log in. And that part works fine however if the username does not exist (and I wrote something that is supposed to catch that) I get a blank white page which is not the intended result. Can someone point out where it is that my code is breaking??

<?php

/*
 Handles Login Requests  
*/

define('DB_HOST', 'localhost');
define('DB_NAME', 'sec_usr');
define('DB_USER', 'sec_usr');
define('DB_PASS', 'n89tzAh2w3Uf4GUu');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());

/*
 $ID=$_POST['user'];
 $Password = $_POST['pass'];
*/

function LogIn()
{
     session_start();
    if(!empty($_POST['user']) && !empty($_POST['pass'])) 
    {
        $query = mysql_query("SELECT * FROM username where userName = '$_POST[user]'") or die(mysql_error());
        $row = mysql_fetch_array($query) or die(mysql_error());
        if(!empty($row['userName']))
        {
            if($row['userPass'] === $_POST['pass'])
            {
                echo"Success"; /* Works */
            }
            else
            {
                echo"Wrong Pass"; /* Works */
            }
        }
        else
        {
            echo"Wrong User"; /* Does Not Work */
        }
    }      
}

if(isset($_POST['submit']))
{
    LogIn();
}

?>

I had added a piece to my question after I had asked the initial question as to how to use the mysqli but my original question which I have reverted the question back to had nothing to do with using mysqli I had figured that if people were telling me to use mysqli I may as well ask those same people how to use it not how to use them together.

</div>
  • 写回答

2条回答 默认 最新

  • doufei5315 2015-06-19 23:04
    关注

    As you pointed out yourself on the comments, the problem is in this part:

    $row = mysql_fetch_array($query) or die(mysql_error());
    

    The PHP function mysql_fetch_array() returns FALSE if there are no rows found. When the user doesn't exist, the query will return FALSE, and PHP will execute the part after or. The problem is that there is no error for mysql_error() to display, since the query executed successfully, that's why you get an empty page.

    To fix it, you could do:

    $row = mysql_fetch_array($query);
    if($row)
    {
        if($row['userPass'] === $_POST['pass'])
        {
            echo "Success";
        }
        else
        {
            echo "Wrong Pass";
        }
    }
    else
    {
        echo "Wrong User";
    }
    

    To be on the safe side, in case you don't receive either the user or the pass variable, you can also add an Exception in the previous if:

    if(!empty($_POST['user']) && !empty($_POST['pass'])) 
    {
        // query the database...
    }
    else
    {
        // Code will most likely not reach here.
        throw new Exception("Form submitted but user/pass not received.");
    }
    

    Also, be aware that mysql_query("SELECT * FROM username where userName = '$_POST[user]'") is open to a SQL injection attack, you must escape the username before using it inside a SQL query. You can read more about it on the mysql_real_escape_string page. Can be done like this (taken from that page):

    $query = sprintf("SELECT * FROM username where userName = '%s'",
        mysql_real_escape_string($_POST[user])
    );
    mysql_fetch_array($query);
    

    And last, the Original MySQL API is deprecated as of PHP 5.5.0, it's recommended that you use MySQLi or PDO_MySQL extension.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog