使用mysql验证在php中登录表单

时间:2022-09-26 07:46:26

when I entered correct password and username my code enters into else statement. When I enter wrong username and password it also goes to else statement. What am I doing wrong with this code? I am using newer version of wamp server software in Windows operating system.

当我输入正确的密码和用户名时,我的代码进入了else语句。当我输入错误的用户名和密码时,它也会转到else语句。这段代码我做错了什么?我在Windows操作系统中使用较新版本的wamp服务器软件。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "php";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}
$login=$_POST['username'];
$passwd=$_POST['passwd'];
if($username!=''&&$passwd!='')
{
    $query="select * from users where login='".$username."' and passwd='".$passwd."'" or die("Connection failed: " . mysqli_connect_error());
    $res=mysqli_fetch_row($query);
    if($res)
    {
        $_SESSION['username']=$login;
        echo "welcome to php";
    }
    else
    {
        echo'You entered username or password is incorrect';
    }
}
else
{
    echo'Enter both username and password';
}

?>
<html>
  <body>
    <form action="login3.php" method="post">
      Username:<br>
      <input type="text" name="username"><br>
      Password:<br>
      <input type="PASSWORD" name="passwd">
      <BR>
      <intput type="submit" value="login" name="submit"><INPUT Type="submit  "value="reset">
    </form> 
  </body>
</html>

2 个解决方案

#1


1  

First of all there is lots of wrong things in your code just refer @David comment. I think you did not fire the query. Here is the code to be modified:

首先,你的代码中有很多错误的东西,只需参考@David评论。我认为你没有解雇查询。这是要修改的代码:

if($login!=''&& $passwd!='')
{
 $query="select * from users where login='".$login."' and passwd='".$passwd."'";

 $result=mysqli_query($conn,$query); 

 if(!$result)
    die("Query Failed: " .  mysqli_error($conn));
 else{
     if(mysqli_num_rows($result)>0)
     {
        $_SESSION['username']=$login;
        echo "welcome to php";
     }
    else
    {
       echo'You entered username or password is incorrect';
     }
 }
}

#2


0  

You are currently trying to login using your database username (root) instead of username from POST.

您当前正在尝试使用数据库用户名(root)而不是POST的用户名登录。

Here is you error:

这是你的错误:

if($username!=''&&$passwd!='')

Should be:

if($login!=''&&$passwd!='')

#1


1  

First of all there is lots of wrong things in your code just refer @David comment. I think you did not fire the query. Here is the code to be modified:

首先,你的代码中有很多错误的东西,只需参考@David评论。我认为你没有解雇查询。这是要修改的代码:

if($login!=''&& $passwd!='')
{
 $query="select * from users where login='".$login."' and passwd='".$passwd."'";

 $result=mysqli_query($conn,$query); 

 if(!$result)
    die("Query Failed: " .  mysqli_error($conn));
 else{
     if(mysqli_num_rows($result)>0)
     {
        $_SESSION['username']=$login;
        echo "welcome to php";
     }
    else
    {
       echo'You entered username or password is incorrect';
     }
 }
}

#2


0  

You are currently trying to login using your database username (root) instead of username from POST.

您当前正在尝试使用数据库用户名(root)而不是POST的用户名登录。

Here is you error:

这是你的错误:

if($username!=''&&$passwd!='')

Should be:

if($login!=''&&$passwd!='')