【问题标题】:Gives out {"error":true,"error_msg":"User already existed with abc@gmail.com"} even though user doesn't exists给出 {"error":true,"error_msg":"User already exists with abc@gmail.com"} 即使用户不存在
【发布时间】:2015-12-04 09:44:10
【问题描述】:

我已经尝试了一个用户注册代码。问题是它给了我 {"error":true,"error_msg":"User already exists with abc@abc.com"} 即使用户不存在在数据库中..请帮我解决这个问题..如果我错了,请原谅我..! 这是我的 /DB_Function.php/code

            <?php
           class DB_Functions {
             private $db;

        // constructor
        function __construct() {
            try {
                $hostname = "localhost";
                $dbname = "miisky";
                $dbuser = "root";
                $dbpass = "";
                $this->db = new PDO("mysql:host=$hostname;dbname=$dbname", $dbuser, $dbpass);
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();
            }
        }

        /**
          * Storing new user
          * returns user details
          */
      public function storeUser($fname, $lname, $email, $password, $mobile) {  
        try {
            $hash = md5($password);
            $sql = "INSERT INTO users(fname, lname, email, password, mobile, created_at) VALUES ('$fname', '$lname', '$email', '$hash', '$mobile', NOW())";
            $dbh = $this->db->prepare($sql);

            if($dbh->execute()){
                // get user details
                $sql = "SELECT * FROM users WHERE email = '$email' LIMIT 1";
                $dbh = $this->db->prepare($sql);
                $result = $dbh->execute();
                $rows = $dbh->fetch();
                $n = count($rows);
                if($n){
                    return $rows;
                }
            }
        }
        catch (Exception $e) {
            echo 'Error accessing database: ' . $e->getMessage();
        }
        return false;
    }
       public function isUserExisted($email) {
        try{
            $sql = "SELECT email FROM users WHERE email = $email LIMIT 1";
            $dbh = $this->db->prepare($sql);
            $result = $dbh->execute();
            $rows = $dbh->fetch();
            $n = count($rows);
            if($n>0){
                return true;
            }else{
                return;
            }
        }
        catch (Exception $e) {
            echo 'Error accessing database: ' . $e->getMessage();
        }
       }
    }
    ?>

这里是我的 /*register.php 代码 */

<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => false);
if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['mobile'])) {
    // receiving the post params
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $mobile = $_POST['mobile'];

if ($db->isUserExisted($email)) {
        // user already existed
        $response["error"] = true;
        $response["error_msg"] = "User already existed with " . $email;
        echo json_encode($response);
    } else {
    // create a new user
    $user = $db->storeUser($fname, $lname, $email, $password, $mobile);
    if ($user) {
        // user stored successfully
        $response["error"] = false;
        $response["uid"] = $user["id"];
        $response["user"]["fname"] = $user["fname"];
        $response["user"]["lname"] = $user["lname"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["created_at"] = $user["created_at"];
        $response["user"]["updated_at"] = $user["updated_at"];
        echo json_encode($response);
    } else {
        // user failed to store
        $response["error"] = true;
        $response["error_msg"] = "Unknown error occurred in registration!";
        echo json_encode($response);
    }
    }
} else {
    $response["error"] = true;
    $response["error_msg"] = "Required parameters (fname, lname, email, password or mobile) is missing!";
    echo json_encode($response);
}
?>

【问题讨论】:

  • $db-&gt;isUserExisted($email) 的返回值是多少?或者您可以将此功能添加到您的问题中吗?
  • 正确使用准备好的语句......不要将变量值直接注入语句中,尤其是未引用的字符串...... BIND $email

标签: php json database pdo


【解决方案1】:

您应该返回truefalse,具体取决于是否在数据库中找到了用户,以及isUserExisted() 函数中的小语法错误。你的isUserExisted() 函数应该是这样的:

// your code

public function isUserExisted($email) {
    try{
        $sql = "SELECT email FROM users WHERE email = '$email' LIMIT 1";
        $dbh = $this->db->prepare($sql);
        $result = $dbh->execute();
        if($dbh->fetch()){
            return true;
        }else{
            return false;
        }
    }catch (Exception $e) {
        die('Error accessing database: ' . $e->getMessage());
    }
}

// your code

【讨论】:

  • 先生..还是遇到同样的问题..{"error":true,"error_msg":"用户已经存在abc@abc.com"}
【解决方案2】:

你有错误的查询语法,使用以下:

插入用户(fname、lname、电子邮件、密码、手机、created_at) 值($fname、$lname、$email、$hash、$mobile、NOW())

当我们在双引号中使用 php 变量时,会出现该变量的值,而在单引号中会出现该变量名。例如:

$x = "hello";
echo "The value is $x"; // The value is hello
echo 'The value is $x'; // The value is $x

现在您可以看到可以在哪里更正您的代码。

【讨论】:

    【解决方案3】:

    您需要在电子邮件中添加引号,如果在您的数据库中未找到电子邮件,则返回 false。最好使用bindParamrowCount() 来计算查询返回的行数

    $sql = "SELECT email FROM users WHERE email = :email LIMIT :val ";
                $dbh = $this->db->prepare($sql);
                $dbh->bindParam(':email', $email, PDO::PARAM_STR);
                $dbh->bindParam(':val', 1, PDO::PARAM_INT);
                $dbh->execute();
                $n  = $dbh->rowCount();
                if($n>0){
                    return TRUE;// return true here
                }else{
                    return FALSE;// return false if not found in database
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 2014-10-11
      • 2016-03-07
      • 2019-12-14
      相关资源
      最近更新 更多