<!DOCTYPE HTML>
<html>
<head>
<title>Sign-In</title>
</head>
<body id="body-color">
<div id="Sign-In">
<form method="POST" action="connectivity.php">
User <br><input type="text" name="user" size="40"><br>
Password <br><input type="password" name="pass" size="40"><br>
<input id="button" type="submit" name="submit" value="Log-In">
</form>
</fieldset>
</div>
</body>
</html>
以上是我用于简单用户登录的表单的html代码。
<?php
define('DB_HOST', 'localhost:3306');
define('DB_NAME', 'sample');
define('DB_USER','user');
define('DB_PASSWORD','passwd');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
function SignIn()
{
session_start(); //starting the session for user profile page
if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM UserName where userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['userName']) AND !empty($row['pass']))
{
$_SESSION['userName'] = $row['pass'];
echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
}
else
{
echo "SORRY... YOU ENTERED WRONG ID AND PASSWORD... PLEASE RETRY...";
}
}
}
if(isset($_POST['submit']))
{
SignIn();
}
?>
这是我用来验证用户登录的php脚本。但是当我输入以前存储在数据库中的正确用户名和密码时,上面的php代码会正确响应。但是当我尝试输入错误的用户名和密码时,else部分无效。相反,它显示一个白色的屏幕。我在哪里可以犯这个错误?
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
返回与获取的行对应的字符串数组,或 如果没有更多行
,则为FALSE
因此请检查FALSE是否为空。
if(($row['userName'])!==FALSE AND ($row['pass'])!==FALSE)
答案 1 :(得分:0)
mysql_fetch_array
返回false
时,您的代码失败。只需从empty
语句中删除if
,您将检查查询是否返回了一条或多条记录并填充了数组。