2012-01-12 68 views
1

下面的使用PDO的MYSQL INSERT命令只是失敗。在失敗並報告之後,DROP TABLE命令也失敗。我只是不能有什麼錯的代碼:/PHP:無法使用PDO插入到MYSQL表中

$sql = $db2->prepare(
'INSERT INTO citizens_12_01_12 
(login, rank, xp, level, citizenship, totalDamage, economySkill, damageToday, strength) 
VALUES 
(:login, :rank, :xp, :citizenship, :totalDamage, :economySkill, :damageToday, :strength)' 
); 
$sql->bindParam(':strength', $api->strength, PDO::PARAM_INT); 
$sql->bindParam(':damageToday', $api->damageToday, PDO::PARAM_INT); 
$sql->bindParam(':economySkill', $api->economySkill, PDO::PARAM_INT); 
$sql->bindParam(':totalDamage', $api->totalDamage, PDO::PARAM_INT); 
//$sql->bindValue(':organization', api->organization, PDO::PARAM_BOOL); 
$sql->bindParam(':citizenship', $api->citizenship, PDO::PARAM_STR); 
$sql->bindParam(':level', $api->level, PDO::PARAM_INT); 
$sql->bindParam(':xp', $api->xp, PDO::PARAM_INT); 
$sql->bindParam(':login', $api->login, PDO::PARAM_STR); 
$sql->bindParam(':rank', $api->rank, PDO::PARAM_STR); 

if ($sql->execute()) { 
    echo "Query succeeded."; 
} else { 
    echo "Query failed."; 
    $db2->query("DROP TABLE 'citizens_12_01_12'"); 
} 

跟蹤誤差在error_log中:55

PHP Warning: PDOStatement::execute() [<a href='pdostatement.execute'>pdostatement.execute</a>]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in .../getcitizens.php on line 55 

線是$ SQL->的execute()行

+0

你能後的錯誤?如果沒有(通過GUI輸出給您),請檢查您的錯誤日誌。 – 2012-01-12 01:43:07

+0

更新OP錯誤 – 2012-01-12 01:46:02

回答

6

你是在insert語句中缺少變量:level

它應該是:

$sql = $db2->prepare(
'INSERT INTO citizens_12_01_12 
(login, rank, xp, level, citizenship, totalDamage, economySkill, damageToday, strength) 
VALUES 
(:login, :rank, :xp, :level, :citizenship, :totalDamage, :economySkill, :damageToday, :strength)' 
); 
+1

耶穌。這就是爲什麼我不應該在接近2點的時候編碼!非常感謝你。 – 2012-01-12 01:48:53

+2

@ J.B。我明白這感受... – jeroen 2012-01-12 01:51:43