注册表单通过db.php连接到数据库,但我在提交登录详细信息时遇到问题。
<html>
<head>
<?php
include('db.php');
$username = @$_POST['username'];
$password = @$_POST['password'];
$submit = @$_POST['submit'];
主要问题是在现有用户点击提交按钮后,它应该给出消息,但 if 语句中存在问题,因为在 wamp服务器< / b>仅显示 else 消息,即错误。
if ($submit)
{
$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
if (mysql_num_rows($result)) {
$check_rows = mysql_fetch_array($result);
$_POST['username'] = $check_rows['username'];
$_POST['password'] = $check_rows['password'];
echo "<center>";
echo "You are now Logged In. ";
echo "</center>";
}
else {
echo "<center>";
echo "No User found. ";
echo "</center>";
}
}
else echo "Error";
?>
</head>
<body>
<form method="post">
Username : <input name="username" placeholder="Enter Username" type="text"><br></br>
Password : <input name="password" placeholder="Enter Password" type="password"><br>
<input type="submit" value="Submit">
</body>
</html>
答案 0 :(得分:1)
您希望获取名为$_POST
的{{1}},但不要将其发送到表单
尝试更改
submit
到
<input type="submit" value="Submit">
答案 1 :(得分:0)
首先这是旧式的php / mysql。所以看看php.net上的PDO看到你正在开始新项目,现在变得非常难以做出改变而不是更晚。
现在谈谈你的问题。如果你打算继续用你的旧方法试试这个。
$sql = "SELECT * FROM user WHERE username=' . $username . ' AND password=' . $password . '";
// check the query with the die & mysql_error functions
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_num_rows($query);
// checking here equal to 1 In a live case, for testing you could use >= but not much point.
if ($result == 1) {
// Checking needs to be Assoc Now you can use the field names,
// otherwise $check_rows[0], $check_rows[1] etc etc
$check_rows = mysql_fetch_assoc($query); // oops we all make mistakes, query not result, sorry.
// This is bad but for example il let this by,
// please dont access user supplied data without
// validating/sanitising it.
$_POST['username'] = $check_rows['username'];
$_POST['password'] = $check_rows['password'];
} else {
// do not logged in here
}
PDO中的相同
$sql=" Your query here ";
$pdo->query($sql);
$pdo->execute();
$result = $pdo->fetch();
if ($result = 1) {
// do login stuff
} else {
// no login
}
请记住,您需要设置PDO,默认情况下它可能在您的服务器上不可用(较旧的php / mysql版本),但您的主机应该很高兴设置它们。