我正在使用PDO将一些数据推送到我的MySQL服务器,表名为'User',我收到以下错误:
[26-Mar-2017 22:18:21 America/Chicago] Could not successfully add user!
[26-Mar-2017 22:18:21 America/Chicago] SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
我已经查看了有关堆栈溢出的其他多个问题,并显示了相同的错误消息,并且归结为PDO准备并执行具有不匹配的参数。但是,我的准备和执行是匹配的,我已经检查了很多次,但我仍然得到错误。这是我的代码:
$noMusicScore = $data->noMusicScore;
$classicalMusicScore = $data->classicalMusicScore;
$popMusicScore = $data->popMusicScore;
$hiphopMusicScore = $data->hiphopMusicScore;
try
{
$db = new PDO('info here......');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $db->prepare("INSERT INTO User (no-music-score, classical-music-score, pop-music-score, hiphop-music-score)
VALUES (:no-music-score, :classical-music-score, :pop-music-score, :hiphop-music-score)");
$query->execute(array(':no-music-score' => $noMusicScore, ':classical-music-score' => $classicalMusicScore,
':pop-music-score' => $popMusicScore, ':hiphop-music-score' => $hiphopMusicScore));
error_log("Successfully added user!");
}
catch(PDOException $e)
{
error_log("Could not successfully add user!");
error_log($e->getMessage());
}
unset($db, $query);
答案 0 :(得分:0)
我解决了这个问题。我只是删除了占位符名称的破折号,一切都落到了位置。 '不'也是一个SQL关键字,所以我认为这也是问题的一部分。