PHP/MySQL 注册表未检测密码

标签 php mysql authentication registration

我正在尝试为我的大学作业制作注册表和登录表单,但注册表似乎没有在密码字段中检测到密码,它总是抛出“需要密码”错误,这是它应该做的如果该字段中未输入任何内容,则执行此操作。

代码如下: 服务器.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/

相关文章:

php - 在 $_SESSION 中存储数据与 memcache(d) 有何不同?

jakarta-ee - Java Web 应用程序 : Using a custom realm

java - 自定义身份验证提供程序没有被称为 Spring Security

security - 刷新 token 和 JWT token 交互

php - 统计mysql表中多列总和等于0的记录条数

php - 如何在 woocommerce 中为购物车创建自定义折扣

php - REGEXR 帮助 - 如何从字符串中提取年份

php - 什么样的代码可以进入PDO交易?

PHP 和 MYSQL "while"只打印一个值

java - 未在数据库中创建的实体