mysqli fetch_assoc()没有发布

时间:2018-06-12 03:41:37

标签: php mysql

我有以下代码并且fetch_assoc无法正常工作,当我手动输入电子邮件地址和哈希时,它会发布活动=' 1'很高兴见下面的代码任何帮助将不胜感激。

<?php

session_start();

require_once("db.php");

$hash = mysqli_real_escape_string($comm, $_POST["token"]);
$email = mysqli_real_escape_string($comm, $_POST["email"]);

$sql = "SELECT * FROM users WHERE email='$email' AND hash='$hash'";
$result = $conn->query($sql);

if ($result->num_rows == 0) {
     $row = $result->fetch_assoc();

     if ($row['active'] == '1') {
          echo 'You Have Already Activated Your Account';
     } else {
          $sql1 = "UPDATE users SET active='1' WHERE email='$email' AND hash='$hash'";

          // If i use the email address and the hash then it works
          // $sql1 = "UPDATE users SET active='1' WHERE email='email@mydomain.com' AND hash='28a78fea4088711fc7a2bb1a6abeb3aa'";

          if ($conn->query($sql1)) {
               $SESSION['userActivated'] = true;
               header("Location: login.php");
               exit();
          }
     }
} else {
     echo 'Token Mismatch!';
}

1 个答案:

答案 0 :(得分:0)

按如下所示更改您的代码,它将起作用

<?php

 session_start();

 require_once("db.php");

 $hash = mysqli_real_escape_string($comm, $_POST["token"]);
 $email = mysqli_real_escape_string($comm, $_POST["email"]);

 $sql = "SELECT * FROM users WHERE email='$email' AND hash='$hash'";
 $result = $conn->query($sql);


 if ($result->num_rows > 0) {

      $row = $result->fetch_assoc();

      if ($row['active'] == '1') {
       echo 'You Have Already Activated Your Account';
      } else {
            $sql1 = "UPDATE users SET active='1' WHERE email='$email' AND hash='$hash'";

           // If i use the email address and the hash then it works
           // $sql1 = "UPDATE users SET active='1' WHERE email='email@mydomain.com' AND hash='28a78fea4088711fc7a2bb1a6abeb3aa'";

           if ($conn->query($sql1)) {
                $SESSION['userActivated'] = true;
                header("Location: login.php");
                exit();
            }
      }
 } else {
      echo 'Token Mismatch!';
 }

?>