无法在数据库$ count!= 1中写入数据

时间:2014-12-22 16:59:17

标签: php mysql pdo

我目前正在开展一个项目,但由于一个不断出现的错误而无法继续,我不知道为什么,但也许你们会知道。当我填写表单并希望将数据插入数据库时​​,我得到错误反馈帐户创建失败。以下是哪个。

所以这是代码:

    // write new users data into database
        $sql = "INSERT INTO users (user_name, user_password_hash, user_email, user_creation_timestamp, user_activation_hash, user_provider_type, user_persnaam, user_bondsnummer, user_telefoonnummer, user_leeftijd, user_enkelsterkte, user_dubbelsterkte, user_geslacht)
                VALUES (:user_name, :user_password_hash, :user_email, :user_creation_timestamp, :user_activation_hash, :user_provider_type, :user_persnaam, :user_bondsnummer, :user_telefoonnummer, :user_leeftijd, :user_dubbelsterkte, :user_enkelsterkte, :user_geslacht)";
        $query = $this->db->prepare($sql);
        $query->execute(array(':user_name' => $user_name,
                              ':user_password_hash' => $user_password_hash,
                              ':user_email' => $user_email,
                              ':user_persnaam' => $user_persnaam,
                              ':user_bondsnummer' => $user_bondsnummer,
                              ":user_telefoonnummer" => $user_telefoonnummer,
                              ":user_enkelsterkte" => $user_enkelsterkte,
                              ":user_dubbelsterkte" => $user_dubbelsterkte,
                              ":user_leeftijd" => $user_leeftijd,
                              ':user_geslacht' => $user_geslacht,
                              ':user_creation_timestamp' => $user_creation_timestamp,
                              ':user_activation_hash' => $user_activation_hash,                                  
                              ':user_provider_type' => 'DEFAULT'));
        $count =  $query->rowCount();
        if ($count != 1) {
            $_SESSION["feedback_negative"][] = FEEDBACK_ACCOUNT_CREATION_FAILED;
            return false;
        }

问题似乎是我无法在我的数据库中写入任何内容,但我确信我可以连接并更新所有内容但不会写入。所以问题不在于与数据库的连接。我认为它与rowcount有关,但也许你们知道什么是错的。双引号不是我已经检查过的问题。

提前致谢

2 个答案:

答案 0 :(得分:0)

我不知道这是否有用,@fred给了你一些线索,你说你确定连接是好的。这是我抓到的:

INSERT INTO users 
            (user_name, 
             user_password_hash, 
             user_email, 
             user_creation_timestamp, 
             user_activation_hash, 
             user_provider_type, 
             user_persnaam, 
             user_bondsnummer, 
             user_telefoonnummer, 
             user_leeftijd, 
             user_enkelsterkte,  /* here user_enkelsterkte is first */
             user_dubbelsterkte, /* here user_dubbelsterkte is second */
             user_geslacht) 
VALUES      (:user_name, 
             :user_password_hash, 
             :user_email, 
             :user_creation_timestamp, 
             :user_activation_hash, 
             :user_provider_type, 
             :user_persnaam, 
             :user_bondsnummer, 
             :user_telefoonnummer, 
             :user_leeftijd, 
             :user_dubbelsterkte, /* here user_dubbelsterkte is first */
             :user_enkelsterkte,  /* here user_enkelsterkte is second */
             :user_geslacht)

如果列类型或长度不匹配,则会失败。

以下是编写代码的更好方法:

$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($query = $this->db->prepare($sql)) { 
  // put code here
}else{
    echo "\nPDO::errorInfo():\n"; 
    print_r($this->db->errorInfo()); 
}

您可以对连接prepare()execute()进行错误检查。

现在您应该知道是否收到错误。

答案 1 :(得分:0)

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ('sebbz', '$2y$10$gEeDMLZQ.c.Mv/nRv6miJebw3/PAPpeCU' at line 1' in C:\xampp\htdocs\php-login\application\models\login_model.php:529 Stack trace: #0 C:\xampp\htdocs\php-login\application\models\login_model.php(529): PDOStatement->execute(Array) #1 C:\xampp\htdocs\php-login\application\controllers\login.php(239): LoginModel->registerNewUser() #2 C:\xampp\htdocs\php-login\application\libs\Application.php(52): Login->register_action() #3 C:\xampp\htdocs\php-login\index.php(27): Application->__construct() #4 {main} thrown in C:\xampp\htdocs\php-login\application\models\login_model.php on line 

我得到了错误。要去搜索问题。也许有人比我快。如果是这样,不要害怕告诉;)