我添加了不允许空数据通过的代码后,我的 PHP 表单不再接受数据。有什么更好的方法可以编写此代码吗?

我添加了不允许空数据通过的代码后,我的 PHP 表单不再接受数据。有什么更好的方法可以编写此代码吗?

所以我的表格不接受空数据,这当然是好的,但是当我刚刚添加用户名和电子邮件时,它会在没有密码的情况下接受它。

我以为通过添加这段代码$username = $email = $password = "";,一旦所有字段都填写完毕,它就会接受新的条目,但现在,即使所有字段都填写完毕,它也不接受任何条目。

看来我添加的代码不允许任何新条目,这不是我的本意。有什么更简单的代码可以完成我在这里尝试做的事情吗?

<?php 

    include('connection.php');

    if(isset($_POST["add"])) {

        // build a function that validates data
         function validateFormData($formData) {
            $formData = trim(stripslashes(htmlspecialchars($formData)));
            return $formData;
        }

        // check to see if inputs are empty
        // create variables with form data
        // wrap the data with our function

        // set all variables to empty by default
        $username = $email = $password = "";

        if(!$_POST["username"]) {
            $nameError = "Please enter a username <br>";
        } else {
            $name = validateFormData($_POST["username"]);
        }

        if(!$_POST["email"]) {
            $emailError = "Please enter your email <br>";
        } else {
            $email = validateFormData($_POST["email"]);
        }

        if(!$_POST["password"]) {
            $passwordError = "Please enter a password <br>";
        } else {
            $password = validateFormData($_POST["password"]);
        }

        // check to see if each variable has data
        if($username && $email && $password) {
                $query = "INSERT INTO users (id, username,  password,   email, signup_date, biography) VALUES (NULL,  '$username', '$password',     '$email', CURRENT_TIMESTAMP,  NULL)";

            if(mysqli_query($conn, $query)) {
                echo "<div class='alert alert-success'>New record in database!</div>";
            } else {
                echo "Error: ".$query."               <br>".mysqli_error($conn);
            }    
        }

    }



    /*
    MY SQL INSERT QUERY

    INSERT INTO users (id, username, password, email, signup_date,     biography) VALUES (NULL, 'jacksonsmith', 'abc123', '[email protected]',   CURRENT_TIMESTAMP, 'Hello! I'm Jackson. This is my bio.');

    */

    ?>




    <!DOCTYPE html>

    <html>

        <head>

            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">

            <title>MySQL Insert</title>

            <!--Bootstrap CSS-->
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

            <!--HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries-->
            <!--WARNING: Respond.js doesn't work if you view the page via file://-->
            <!--[if lt IE 9]>
                <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
                <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
                <![endif]-->
         </head>

        <body>
            <div class="container">
                <h1>MySQL Insert</h1>


            <p class="text-danger">* Required fields</p>
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post">
                    <small class="text-danger">*<?php echo $nameError; ?></small>
                    <input type="text" placeholder="Username" name="username"><br><br>

                    <small class="text-danger">* <?php echo $emailError; ?></small>
                    <input type="text" placeholder="Email" name="email">  <br><br>

                    <small class="text-danger">* <?php echo $passwordError; ?></small>
                    <input type="password" placeholder="Password" name="password"><br><br>

                    <input type="submit" name="add" value="Add Entry">
                </form>

            </div>

            <!--jQuery -->
            <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

            <!--Bootstrap JS-->
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>     
        </body>
    </html>

答案1

$name = validFormData($_POST["用户名"]);

看起来$name应该$username在这一行,否则$username在你检查它以决定是否要执行 SQL 查询时它总是空白的。

相关内容