简体   繁体   English

PDO检查用户是否已输入所有字段

[英]PDO checking if user has enter all fields

I can't seem to send a error message to my users who leave a blank field. 我似乎无法向保留空白字段的用户发送错误消息。 How can I go about this? 我该怎么办? EX: if they forget to enter a password: echo FORGOT PASSWORD and etc. 例如:如果他们忘记输入密码,则回显FORGOT PASSWORD等。

<h1>Register</h1>
<form action="register.php" method="post">
    <input type="text" name="username" placeholder="username"><br />
    <input type="password" name="password" placeholder="password"><br />
    <input type="submit" value="submit">
</form>

<?php
    if(isset($_POST['username']) && isset($_POST['password'])){
        require 'core/connect.php';

            $query = dbConnect()->prepare("INSERT INTO `users`(username, password) VALUES(:username, :password)");

                $query->bindParam(':username', $_POST['username']);
                $query->bindParam(':password', $_POST['password']);

                if($query->execute()){
                    header("Location: index.php");
                } else{
                    echo 'ERROR';
                }
    }
?>

If you want to check whether the fields are blank, use empty() : 如果要检查字段是否为空,请使用empty()

if(isset($_POST['username']) && isset($_POST['password'])){
    if (empty($_POST['username']) || empty($_POST['password'])) {
        echo "Username and password must be filled in";
    } else {
        // Code to insert into database and redirect
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM