您好我正在尝试创建自己的简单登录系统。我连接到mysql服务器,现在想查找用户名是否在我在mysql数据库上创建的用户图表中。如果是让他们通过成员区域。我到底该怎么做?
<?php
$mysql_host = "mysql14.000webhost.com";
$mysql_database = "a6981734_Users";
$mysql_user = "XXXXX";
$mysql_password = "XXXXX";
$account = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database, $account);
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM Users";
$result = mysql_query($sql, $account);
while($row = mysql_fetch_array($result)) {
$username1 = $row['Username'];
if($username1 == $username) {
echo "Welcome";
break;
} else {
echo "Username / Password Not Found In Database";
break;
}
}
?>
HTML LOGIN FORM!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
</head>
<body>
<form action="PHPVALIDATE.php" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" />
</body>
</html>
答案 0 :(得分:0)
将Where
添加到您的查询
$sql = "SELECT * FROM Users WHERE Username='".$username."' AND Password='".$password."'";
//Assuming your column name is Password to store your password.
附加说明了解mysqli
或PDO
作为mysql
被取消。
更新了答案
<?php
error_reporting(E_ALL);
$mysql_host = "mysql14.000webhost.com";
$mysql_database = "a6981734_Users";
$mysql_user = "XXXXX";
$mysql_password = "XXXXX";
$account = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database, $account);
$username = $_POST['username'];
$password = $_POST['password'];
echo $username.'<br/>';
echo $password.'<br/>';
$sql = "SELECT * FROM Users WHERE Username='".$username."' AND Password='".$password."'";
$result = mysql_query($sql, $account) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$username1 = $row['Username'];
$password1=$row['Password'];
echo $username1.'<br/>';
echo $password1.'<br/>';
}
?>
答案 1 :(得分:0)
这是完整的代码
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM Users WHERE username='$username' AND password='$password' LIMIT 1";
$result = mysql_query($sql, $account);
$num_rows=mysql_num_rows($result);
if($num_rows>0){
while($row = mysql_fetch_array($result)) {
$username1 = $row['username'];
$password1= $row['password'];
$username1=$_SESSION['username'];
header("location:member_page?u='.$username1.'");
} else {
echo "Username / Password Not Found In Database";
}
}
}
header()会将用户重定向到您希望他们发送的区域(页面)。
希望有所帮助。让我知道是否有效