-2

我是 PDO 的新手,正在尝试注册和登录。
当我尝试调试它时,它返回 bool(false) 。无法找出为什么会这样。

<?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.';

          }


?>
4

2 回答 2

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));
于 2013-07-12T06:35:00.533 回答
0

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

$fetchUsers->fetchAll() != $username所以从 if 条件中删除代码。然后检查。也许它会起作用。

于 2013-07-12T06:30:58.873 回答