按钮类型提交不向服务器发送数据
Button type submit not sending data to the server
我是 PHP 的新手。我正在制作一个登录表单。
这是我的 html:
<form method="POST" id="login_form" action="log_in.php">
<div class="form-group">
<label for="Username">Username</label>
<input autocomplete="off" type="text" class="form-control" id="username" required>
</div>
<div class="form-group">
<label for="Password">Password</label>
<input autocomplete="off" type="password" class="form-control" id="password" required>
</div>
<button type="submit" id="submit_login" class="btn">Log in</button>
</form>
这是我的 log_in.php
<?php
include("db.php");
if ($_SERVER['REQUEST_METHOD']=='POST') {
$username = $_POST["username"];
$password = $_POST["password"];
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND pass=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();
$check = mysqli_num_rows($result);
if ($check > 0) {
while ($row = $result->fetch_assoc()) {
if ($username == $row['username'] && (password_verify($password, $row['pass']) || $password == $row['pass'])) {
if ($row['stat'] == "Admin") {
$conn->close();
header("Location: admin.php");
break;
} else {
$conn->close();
header("Location: member.php");
break;
}
}
}
}
}
else {
echo "<script>";
echo "alert('No Matching Records!');";
echo "</script>";
}
?>
问题是我认为按钮没有向服务器提交用户名和密码。我得到一个错误 Undefined index:username 和 Undefined index:password。它不会登录并加载到 header 目的地。我找不到问题所在。提前谢谢你!
为您的输入添加名称:
<form method="POST" id="login_form" action="log_in.php">
<div class="form-group">
<label for="username">Username</label>
<input autocomplete="off" type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input autocomplete="off" type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" id="submit_login" class="btn">Log in</button>
</form>
还要注意这个
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND pass=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
如果您在数据库中散列您的密码,将永远不会 return 任何东西(我希望您这样做?!)。您应该将其从查询中删除。
$stmt = $conn->prepare("SELECT * FROM users WHERE username=?");
$stmt->bind_param("s", $username);
$stmt->execute();
有关详细信息,请参阅 password_hash documentation。
我是 PHP 的新手。我正在制作一个登录表单。 这是我的 html:
<form method="POST" id="login_form" action="log_in.php">
<div class="form-group">
<label for="Username">Username</label>
<input autocomplete="off" type="text" class="form-control" id="username" required>
</div>
<div class="form-group">
<label for="Password">Password</label>
<input autocomplete="off" type="password" class="form-control" id="password" required>
</div>
<button type="submit" id="submit_login" class="btn">Log in</button>
</form>
这是我的 log_in.php
<?php
include("db.php");
if ($_SERVER['REQUEST_METHOD']=='POST') {
$username = $_POST["username"];
$password = $_POST["password"];
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND pass=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();
$check = mysqli_num_rows($result);
if ($check > 0) {
while ($row = $result->fetch_assoc()) {
if ($username == $row['username'] && (password_verify($password, $row['pass']) || $password == $row['pass'])) {
if ($row['stat'] == "Admin") {
$conn->close();
header("Location: admin.php");
break;
} else {
$conn->close();
header("Location: member.php");
break;
}
}
}
}
}
else {
echo "<script>";
echo "alert('No Matching Records!');";
echo "</script>";
}
?>
问题是我认为按钮没有向服务器提交用户名和密码。我得到一个错误 Undefined index:username 和 Undefined index:password。它不会登录并加载到 header 目的地。我找不到问题所在。提前谢谢你!
为您的输入添加名称:
<form method="POST" id="login_form" action="log_in.php">
<div class="form-group">
<label for="username">Username</label>
<input autocomplete="off" type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input autocomplete="off" type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" id="submit_login" class="btn">Log in</button>
</form>
还要注意这个
$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND pass=?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
如果您在数据库中散列您的密码,将永远不会 return 任何东西(我希望您这样做?!)。您应该将其从查询中删除。
$stmt = $conn->prepare("SELECT * FROM users WHERE username=?");
$stmt->bind_param("s", $username);
$stmt->execute();
有关详细信息,请参阅 password_hash documentation。