简体   繁体   English

PHP:无法使用PDO插入MYSQL表

[英]PHP: cannot INSERT into MYSQL table using PDO

The below MYSQL INSERT command, using PDO, just fails. 下面的MYSQL INSERT命令使用PDO失败。 And after it fails and reports so, the DROP TABLE command also fails. 并且在失败并报告后,DROP TABLE命令也会失败。 I just can't get what's wrong with the code :/ 我只是无法理解代码的问题:/

$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'");
}

Following error in error_log: error_log中出现以下错误:

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

Line 55 is the $sql->execute() line 第55行是$ sql-> execute()行

You are missing your variable :level in the insert statement. 您在插入语句中缺少变量:level

It should be: 它应该是:

$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)'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM