使用PHP和MySQL登录页​​面问题。这是我的代码还是别的什么?

时间:2016-12-28 12:46:17

标签: php mysqli phpmyadmin

我正在学习使用YouTube教程进行编码,我决定建立一个登录系统。当我尝试出来时,我的登录页面给了我错误。我尝试了其他几种方法(使用mysql而不是mysqli),但仍然给了我这些问题。这是我的PHP登录代码:

配置文件: -

<?php 
    define('DB_SERVER', 'localhost'); 
    define('DB_USERNAME', 'root'); 
    define('DB_PASSWORD', ''); 
    define('DB_DATABASE', 'aspire'); 
    $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE‌​); 
?> 

登录页面: -

 <?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT id FROM users WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>

我得到的错误代码是:

  

警告:mysqli_fetch_array()期望参数1为mysqli_result,   给出的布尔值   第13行/Applications/XAMPP/xamppfiles/htdocs/loginaspire/index.php

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   给出的布尔值   第16行/Applications/XAMPP/xamppfiles/htdocs/loginaspire/index.php

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您的Query似乎没有返回任何使用validation的内容

$result = mysqli_query($db,$sql);
if(!empty($result) && $result !=null){
do rest of thing with $result
}else{
echo("Error description: " . mysqli_error($db));
}

注意:还要确保您有数据库连接

答案 1 :(得分:-2)

$count = mysqli_num_rows($result);
if($count > 0){
//Here your code
 $result = mysqli_query($db,$sql);
 $row = mysqli_fetch_array($result,MY`enter code here`SQLI_ASSOC);
 $active = $row['active'];
}else{
echo "No record found";
}