gpt4 book ai didi

php - 使用MySQL验证的php中的登录表单

转载 作者:行者123 更新时间:2023-11-29 01:53:02 25 4
gpt4 key购买 nike

当我输入正确的密码和用户名时,我的代码进入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>

最佳答案

首先,你的代码中有很多错误的地方,只要引用@David comment。我想你没有提出这个问题。下面是要修改的代码:

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';
}
}
}

关于php - 使用MySQL验证的php中的登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36291768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com