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

时间:2013-12-29 09:08:28

标签: php

我似乎无法向留空字段的用户发送错误消息。我怎么能这样做? EX:如果他们忘记输入密码:回复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';
                }
    }
?>

1 个答案:

答案 0 :(得分:0)

如果要检查字段是否为空,请使用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
    }
}