douwei3280 2017-01-13 11:30 采纳率: 0%
浏览 7
已采纳

Php登录工作仅在mysqli_num_rows等于零时才有效,否则不起作用。我卡住了

here's the code guys please help me if mysqli_num_rows==false Than code works but why num rows doesn't work i can't get it i tried everything but same error appears

 <?php
 //Start session
 session_start();

//Include database connection details
require_once('db.php');
    if(isset($_POST['submit'])){
        $username=$_POST['username'];
        $password=$_POST['password'];
        $query="SELECT * FROM users WHERE username='$username' and password='$password'";
        $result=mysqli_query($con,$query);
        if($row=mysqli_num_rows($result)==1){
            mysqli_fetch_array($con,$result);
            echo 'Logged in';
            header('location:profile.php');
        }
        else{
            echo 'error occured';
        }
    }
?>
<form method="POST">
 <input type="text" name="username" placeholder="username">
<input type="text" name="password" placeholder="password">
<input type="submit" name="submit">

</form>
  • 写回答

1条回答 默认 最新

  • douyong7199 2017-01-13 11:43
    关注
    <form method="POST">
     <input type="text" name="username" placeholder="username">
    <input type="text" name="password" placeholder="password">
    <input type="submit" name="submit">
    </form>
    <?php
    error_reporting(E_ALL); // check all type of error
    ini_set('display_errors',1); // display those errors
    session_start();
    require_once('db.php');
    if(!empty(trim($_POST['username'])) && !empty(trim($_POST['password']))){ // check with posetd value
       $user_name = trim($_POST['username']);
       $password = md5(trim($_POST['password']));
       $query = "SELECT * FROM users where username='$username' and password = '$password'"; // don't use plain password, use password hashing mechanism
       $result = mysqli_query($con,$query); // run the query
       if(mysqli_num_rows($result)>0){ // if data comes
            // here do some data assignment into session
            header('location:profile.php'); // go to other page
       }else{
           echo "Login creadentials are not correct"; // else no user is there with the given credentials
       }
    }else{
      echo "please fill the form value";
    }
    ?>
    

    Note:-

    Read and use prepared statements to prevent your code from SQL Injection. :-http://us.php.net/manual/en/mysqli-stmt.prepare.php

    Above file extension must be .php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算