使用mysql验证在php中登录表单
16lz
2021-01-22
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';
}
}
}
更多相关文章
- [php]在PHP中读取和写入WORD文档的代码
- php评论回复无限极嵌套如何实现?已写基本代码,期待高手解惑
- mysqli不执行Select语句
- advertising.php源代码分析
- 什么是客户端准备语句?
- thinkphp友情链接代码
- 如何在php中找到更轻或更暗版本的十六进制代码的十六进制代码
- PHP 网站保存快捷方式的实现代码
- 正则表达式匹配wordpress类似的短代码,用于自闭和封闭。