PHP PDO查询返回bool(false)

时间:2013-07-12 06:22:08

标签: php pdo insert

我是PDO的新手,正在尝试进行注册和登录。这是返回bool(假)
当我试图调试它。无法找出原因。

<?php
       include_once('db.php');

    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = sha1($_POST['password']);
    $confirm_password = sha1($_POST['confirm_password']);
    $ip = $_SERVER['REMOTE_ADDR'];
        $fetchUsers = $db->query("SELECT * FROM `users` WHERE `username` = '$username'");

        if(!empty($username) &&
           !empty($email) &&
           !empty($password) &&
           !empty($confirm_password) &&
           filter_var($email, FILTER_VALIDATE_EMAIL) &&
           $password == $confirm_password &&
           $fetchUsers->fetchAll() != $username) {

                $insertSQL = "INSERT INTO `users`
                             (`username`, `email`, `password`, `ip`) VALUES
                             (:username => '$username', :email => '$email', :password => '$password', :ip => '$ip')";

                    $insertUser = $db->query($insertSQL);
                        var_dump($insertUser);

                echo '<br /><br />You have successfully registered.';


           }

           else {

                echo '<br /><br />There was an error with your registration.';

          }


?>

2 个答案:

答案 0 :(得分:1)

您的INSERT查询错误。也可以使用prepare来准备语句。

$insertSQL = "INSERT INTO `users` (`username`, `email`, `password`, `ip`) 
VALUES (:username,:email,:password,:ip)";

$insertUser = $db->prepare($insertSQL);
$insertUser->execute(array(':username' => $username, ':email' => $email, ':password' => $password, ':ip' => $ip));

答案 1 :(得分:0)

在If条件下,代码$fetchUsers->fetchAll() != $username似乎不正确。 由于fetchAll将返回数组,您将其与字符串进行比较。

所以从if条件中删除$fetchUsers->fetchAll() != $username代码。然后检查。可能会有用。