Php脚本没有';t检查mysql表中的username值


Php script doesn't check the username value in mysql table

我只是PHP新手,我有一个项目仍在构建中,现在正在进行中管理员区域。

现在我正在编写一个脚本,通过查找两个条件来更新表中的密码"username and fname"如果与输入的用户名相同,密码应该会更改,它会成功地更改,但问题是,一旦我输入了错误的用户名,它仍然会更新密码,并且不会显示"您输入的用户名不存在"的消息,而且当我写错fname时,它不会显示消息给,这确实让我在哪里出错,尽管我知道A有些地方错了。

我请求任何人的帮助,我会感激的。

这是我的脚本

  <?php
       session_start(); 
    //include mysqli_connect
    $name = $_POST['fname']; 
    $newp = $_POST['newpword'];
    $user=$_POST['username'];
   $result = mysql_query("SELECT password FROM admin WHERE fname='$name' 
   AND username='$user'  "); 
     if(!$result)  
    {  
    echo "The username you entered does not exist";  
    }  
     elseif(mysql_num_rows($result)>0) 
   $result=mysql_query("UPDATE admin SET password='$newp' where fname='$name'");    
     {  
    echo "Password change successfully";
echo"<br>";
  echo"<a href=index.php> Click here to signin </a>";
exit; 
      }
    {  
    echo "The new password and confirm new password fields must be the same";
  }  
 ?>

您的if语句和方括号在代码中非常混杂。我想我理解你想做什么,不过。。。但是,您应该仔细阅读自己的代码,并对所有内容进行正确的缩进。

我已将您的代码更改为使用pdo

我为旧用户密码添加了POST值,因为在更新用户密码时,即使他们已经登录,你也应该验证这一点。你需要在发送密码的表单中添加一个字段。如果你不想使用它,你只需要去掉代码中的逻辑。

而且-我真的希望你不要把密码存储在纯文本中。如果你是,请在这篇文章下面的评论中告诉我你的确切PHP版本是什么,我可以更新我的答案,向你展示如何存储和使用哈希密码。不过,这确实取决于版本。

<?php
session_start(); 
$_POST['fname']    = 'fname'; 
$_POST['newpword'] = 'newpword';
$_POST['username'] = 'username';
$name = (isset($_POST['fname']))    ? $_POST['fname']    : die("'$_POST['fname'] is not set");  
$newp = (isset($_POST['newpword'])) ? $_POST['newpword'] : die("'$_POST['newpword'] is not set");  
$user = (isset($_POST['username'])) ? $_POST['username'] : die("'$_POST['username'] is not set"); 
// you should get the old password, too, 
// so you can verify that it's the correct user
$_POST['oldpass'] = 'password';
$oldp = (isset($_POST['oldpass'])) ? $_POST['oldpass'] : die("'$_POST['oldpass'] is not set"); 

$pdo = new PDO("mysql:host=localhost;dbname=test", 'root', 'password');
$stmt = $pdo->prepare("SELECT password FROM admin WHERE fname=:fname AND username=:user"); 
$stmt->bindParam(':fname', $name);
$stmt->bindParam(':user',  $user);
$success = $stmt->execute();  
$result  = $stmt->fetch(PDO::FETCH_ASSOC); 
if ($success===false) {
   print "an error occurred in the query <br/>".print_r($stmt->errorInfo(),true); 
}  
elseif ($success!==false && $result===false)
{
   print "that username was not found in the database";
}
else
{  
   if ($result['password']==$oldp)
   { 
      $stmt2 = $pdo->prepare("UPDATE admin SET password=:newp where fname=:fname"); 
      /* You should really HASH this password before storing it*/ 
      $stmt2->bindParam(':newp',  $newp);
      $stmt2->bindParam(':fname', $name);
      $success2 = $stmt2->execute();   
      if ($success2!==false)
      {
         echo "Password change successfully";
         echo"<br>";
         echo"<a href=index.php> Click here to signin </a>"; 
      } 
      else
      { 
         print "an error occurred updating the password <br/>"; 
      }
   }
   else
   {
      print "old password didn't match";
   }
}  
?>

我认为问题出在if($result)条件上。与其检查$result,不如检查if(mysql_num_rows($result)>0)