显示错误消息PHP Mysql

时间:2012-12-17 06:34:41

标签: php mysql

我无法获得最后2个回声,即使更新查询失败,它仍然显示成功。如果有人对此代码有任何建议,请在任何行上进行改进,请执行!

<?php
        if(!empty($_POST['username']) && !empty($_POST['answer']))  { 
            $username = $_POST['username'];
            $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it
            or die(mysql_error());
            $fetched = mysql_fetch_array($idfetch);  
            $id = $fetched['id']; //get users id for checking
            $answer = $_POST['answer'];
            $password = (mysql_real_escape_string($_POST['password']));
            $confpass = (mysql_real_escape_string($_POST['confpass']));
            if ($password != $confpass) {
                echo ("Passwords do not match, please try again.");
                exit;
            }
            $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'");
            if($updatequery)  {  
                echo "<h1>Success</h1>";  
                echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>";  
            }  
            else  {  
                echo "<h1>Error</h1>";  
                echo "<p>Sorry, but a field was incorrect.</p>";  
            }  
       } 
?>

提前致谢!

6 个答案:

答案 0 :(得分:2)

mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'") or die(mysql_error()."update failed");

并使用

mysql_affected_rows()

Returns the number of affected rows on success, and -1 if the last query failed.

答案 1 :(得分:1)

使用try catch并尝试在php中获取错误启用错误报告

<?php
        error_reporting(E_ALL);
        ini_set('display_errors','On');
        if(!empty($_POST['username']) && !empty($_POST['answer']))  { 
        $username = $_POST['username'];
        $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it
        or die(mysql_error());
        $fetched = mysql_fetch_array($idfetch);  
        $id = $fetched['id']; //get users id for checking
        $answer = $_POST['answer'];
        $password = (mysql_real_escape_string($_POST['password']));
        $confpass = (mysql_real_escape_string($_POST['confpass']));
        if ($password != $confpass) {
        echo ("Passwords do not match, please try again.");
        exit;}

        try{
        $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'");
        if($updatequery)  {  
        echo "<h1>Success</h1>";  
        echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>";  }  
        else  {  
        echo "<h1>Error</h1>";  
        echo "<p>Sorry, but a field was incorrect.</p>";  
        }  

        }catch(Exception $e){
            print_R($e);
        }
        }

答案 2 :(得分:0)

使用

if(mysql_num_rows($updatequery)  > 0) {
    // success
} else {
    // error
}

$updatequery将始终为真(非NULL),直到查询中出现错误

答案 3 :(得分:0)

使用或 die(mysql_error()),因为如果您的查询出错,它将显示mysql错误。

$updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'") or die(mysql_error());

答案 4 :(得分:0)

试试这个:

$idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'");
if(!idfetch){
  die(mysql_error());
}

对所有其他查询也一样。

答案 5 :(得分:0)

试试这个,首先将行计数值计算为1,然后继续登录过程。

<?php
    if(!empty($_POST['username']) && !empty($_POST['answer']))  { 
        $username = $_POST['username'];
        $idfetch = mysql_query("SELECT id FROM users WHERE username ='$username'") //check it
        or die(mysql_error());
        $fetched = mysql_fetch_array($idfetch);

        $count= mysql_num_rows($idfetch);

        if($count>0){
        $id = $fetched['id']; //get users id for checking
        $answer = $_POST['answer'];
        $password = (mysql_real_escape_string($_POST['password']));
        $confpass = (mysql_real_escape_string($_POST['confpass']));
        if ($password != $confpass) {
            echo ("Passwords do not match, please try again.");
            exit;
        }

        $updatequery = mysql_query("UPDATE users SET PASSWORD='$password' WHERE id='$id' AND username='$username' AND answer='$answer'");

          if($updatequery)  {  
            echo "<h1>Success</h1>";  
            echo "<p>Your account password was successfully changed. Please <a href=\"login.php\">click here to login</a>.</p>";  
         }  
           else  {  
             echo "<h1>Error</h1>";  
             echo "<p>Sorry, but a field was incorrect.</p>";  
           }  
   } } ?>