我正在尝试为我的大学作业制作注册表和登录表单,但注册表似乎没有在密码字段中检测到密码,它总是抛出“需要密码”错误,这是它应该做的如果该字段中未输入任何内容,则执行此操作。
代码如下: 服务器.php:
<?php
$username = '';
$email = '';
$errors = array();
//connect to the database
$db = mysql_connect('localhost', 'root', '', 'registration');
//if the register button is clicked
if (isset($_POST['register'])) {
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password_1 = mysql_real_escape_string($_POST['password_1']);
$password_2 = mysql_real_escape_string($_POST['password_2']);
//ensure that form fields are filled properly
if (empty($username)) {
array_push($errors, 'Username is required');
}
if (empty($email)) {
array_push($errors, 'An Email is required');
}
if (empty($password_1)) {
array_push($errors, 'A password is required');
}
if ($password_1 != $password_2) {
array_push($errors, 'The two passwords do not match');
}
if (count($errors) == 0) {
$password = md5($password_1);
$sql = "INSERT INTO users (username, email, password)
VALUES ('$username', '$email', '$password')";
mysql_query($db, $sql);
}
}
?>
注册.php:
<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>User Registration using PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo "$username";?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="text" name="email" value="<?php echo "$email";?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<button type="submit" name="register" class="btn">Register</button>
</div>
<p>
Already have an account? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
最佳答案
您必须将表单上的输入标记与 PHP 端的 POST 标记相匹配。如果您将输入标记称为“password”,那么您必须在 PHP 端将其引用为 $_POST['password'],但您将其称为 $_POST['password_1']
关于PHP/MySQL 注册表未检测密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44569572/