2013-10-07 3 views
5

EDIT:タイプuser_idのカラムに数字のある$ user_idを挿入しようとしていた問題を認識した。 int。私は文字列を整数に変換しましたが、まだエラーは分かりませんでしたが、それは時間の制約のためです。このスクリプトの登録クラスには、新しいユーザーをデータベースに追加する関数があり、そのコードは完全に正常に機能します:PHPを介した "挿入" mySQLクエリが機能するようだが、何も追加されない

$query_new_user_insert = $this->db_connection->prepare('INSERT INTO users (user_name, user_password_hash, user_email, user_activation_hash, user_registration_ip, user_registration_datetime) VALUES(:user_name, :user_password_hash, :user_email, :user_activation_hash, :user_registration_ip, now())'); 
$query_new_user_insert->bindValue(':user_name', $user_name, PDO::PARAM_STR); 
$query_new_user_insert->bindValue(':user_password_hash', $user_password_hash, PDO::PARAM_STR); 
$query_new_user_insert->bindValue(':user_email', $user_email, PDO::PARAM_STR); 
$query_new_user_insert->bindValue(':user_activation_hash', $user_activation_hash, PDO::PARAM_STR); 
$query_new_user_insert->bindValue(':user_registration_ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR); 
$query_new_user_insert->execute(); 

// id of new user 
      $user_id = $this->db_connection->lastInsertId(); 

      if ($query_new_user_insert) { 
       // send a verification email 
       if ($this->sendVerificationEmail($user_id, $user_email, $user_activation_hash)) { 
        // when mail has been send successfully 
        $this->messages[] = $this->lang['Verification mail sent']; 
        $this->registration_successful = true; 
       } else { 
        // delete this users account immediately, as we could not send a verification email 
        $query_delete_user = $this->db_connection->prepare('DELETE FROM users WHERE user_id=:user_id'); 
        $query_delete_user->bindValue(':user_id', $user_id, PDO::PARAM_INT); 
        $query_delete_user->execute(); 

        $this->errors[] = $this->lang['Verification mail error']; 
       } 
      } else { 
       $this->errors[] = $this->lang['Registration failed']; 
      } 
で提供されている高度なバージョンのphpログインシステムです。

私は、次のコードを使用して自分のサイトの別の部分のためにそのコードをコピーしてみました:

public function addNewTag($postContent) { 
    if ($this->databaseConnection()) { 
     $query_add_tag = $this->db_connection->prepare('INSERT INTO tags (tag_name, tag_short_name, tag_id, color, user_id, creation_time) 
VALUES(:tag_name, :tag_short_name, :tag_id, :color, :user_id, :creation_time)'); 
     $query_add_tag->bindValue(':tag_name', $postContent['tag_name'], PDO::PARAM_STR); 
     $query_add_tag->bindValue(':tag_short_name', $postContent['tag_short_name'], PDO::PARAM_STR); 
     $query_add_tag->bindValue(':tag_id', rand(100000000000, 999999999999), PDO::PARAM_STR); 
     $query_add_tag->bindValue(':color', $postContent['color'], PDO::PARAM_STR); 
     $query_add_tag->bindValue(':user_id', $user_id, PDO::PARAM_STR); 
     $query_add_tag->bindValue(':creation_time', time(), PDO::PARAM_STR); 
     $query_add_tag->execute(); 
     if ($query_add_tag) { 
      return true; 
     } else { 
      return false; 
     } 
    } else { 
     return false; 
    } 
} 

関数は、コード

$content->addNewTag($_POST); 

と$ postContentのための変数として、$ _POSTと呼ばれています私はそう、関数はtrue( "1")を返しますが、私は自分のデータベースの内容をチェックするとき、何も追加されません。ここで何が問題になるのでしょうか?私はmySQLにはそれほど偉大ではないので、おそらくスクリプト内の別の場所では明らかなエラーです。

+3

です。 'if($ query_add_tag)'をテストすることができないので、実行失敗時には偽の値にならないでしょう –

+0

[PDOエラー処理マニュアル](http://php.net/manual/en/pdo.error)を見てください。 -handling.php)。エラーメソッドを選択してください。デフォルトはサイレントエラーです。これはあなたが見ているものかもしれません。 –

+0

関数の開始時に$ this-> databaseConnectionのvar_dumpを実行すると、何が出力されますか? –

答えて

0

autocommit=0で実行していますか?その場合は、autocommit=1を設定するか、関数の最後にcommitを追加してください。 `$ query_add_tag`は常にPDOStatementオブジェクトです。

$query_add_tag->commit(); 
関連する問題