mysqli_fetch_assoc()未获取任何数据/未获取空数据

时间:2019-03-05 07:48:47

标签: php

因此,我正在为我的网页制作一个管理控制台,但遇到了错误/错误。当我想使用uid作为参考从数据库中获取数据时,我没有任何数据。这是我的代码:

 <?php

  session_start();

  $secretToken = "UWAL2019";

  include '../inc/db.inc.php'; # Including Database Info

  if (!isset($_POST['submit'])) {
    # If access has been sent straight trough using link, not submit button

    header("Location: index.php"); # Sending client back to Login page
    exit(); # (for security purpouses) Exiting this page
  }else {
    # If access has been sent using button

    $uid = $_POST['uid']; # Getting subbmitted Username, using POST method
    $pwd = $_POST['pwd']; # Getting subbmitted Password, using POST method

    if (empty($uid) || empty($pwd)) { # If some fields are empty

      header("Location: index.php?status=empty"); # Sending client back to Login page, with status EMPTY
      exit();

    }else { # If all fields are full
      # Login credentials checking, using Database

      $sql = "SELECT * FROM admins WHERE uid='$uid';";
      $result = mysqli_query($conn, $sql);
      $resultCheck = mysqli_num_rows($result);

      if ($resultCheck > 0) {

       header("Location: index.php?status=wrongCK");
       exit();
     }else {

       if ($data = mysqli_fetch_assoc($result)) {

         if ($data['pwd'] != $pwd) {

           header("Location: index.php?status=wrongRS");
           exit();
         }else {

           $_SESSION['secretToken'] = $data['secretToken'];

           if ($_SESSION['secretToken'] != $secretToken) {

             session_destroy();
             header("Location: index.php?status=wrong_token");
             exit();

           }else {

             $_SESSION['id'] = $data['id'];
             $_SESSION['uid'] = $data['uid'];
             $_SESSION['pwd'] = $data['pwd'];
             $_SESSION['maskName'] = $data['maskName'];
             $_SESSION['email'] = $data['email'];

             header("Location: adminPanel.php");
             exit();
           }
         }
       }else {
         header("Location: index.php?status=fetch_error");
         exit();
       }
     }
    }
  }

 ?>

仅供参考:标头中的“?status”变量是错误代码。我的代码在?status = fetch_error处停止,我无法通过它。有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

我认为您的问题是这一行:

if ($resultCheck > 0) {
    header("Location: index.php?status=wrongCK");
    exit();
}

您基本上是在说“如果找到用户,请重定向”。我想你应该有:

if ($resultCheck < 1) {
    header("Location: index.php?status=wrongCK");
    exit();
}

当找不到用户时将重定向。