我有点困惑,因为我把所有东西放在了一起,而且我已经在这里待了几个小时了。我一直收到这个错误
PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
这是我收集信息的客户端代码段:
$('#card-filter-form').submit(function(e){
e.preventDefault();
var contents = {
card_name : $('#card_name').val(),
card_number : $('#card_number').val(),
setlist__id : $('#setlist__id').val(),
ygo_rarity__id : $('#ygo_rarity__id').val(),
ygo_card_type__id : $('#ygo_card_type__id').val(),
ygo_attribute__id : $('#ygo_attribute__id').val(),
ygo_monster_type__id : $('#ygo_monster_type__id').val(),
level : $('#level').val(),
atk : $('#atk').val(),
def : $('#def').val()
};
var spinner = new Spinner().spin();
var target = document.getElementById('card-search-filter-reset');
target.appendChild(spinner.el);
$.ajax({
url : '../assets/server/cardSearch.php',
type : 'POST',
data : contents,
dataType : 'JSON',
success : function(data){
spinner.stop();
if(!data.errors){
$('#results-table').html(data.table);
}else{
alert(data.message);
}
},
error: function (request, status, error) {
spinner.stop();
console.log(request.responseText);
}
});
});
这是服务器端我的代码片段:
$card_name = $_REQUEST['card_name'];
$card_number = $_REQUEST['card_number'];
$setlist__id = $_REQUEST['setlist__id'];
$ygo_rarity__id = $_REQUEST['ygo_rarity__id'];
$ygo_card_type__id = $_REQUEST['ygo_card_type__id'];
$ygo_attribute__id = $_REQUEST['ygo_attribute__id'];
$ygo_monster_type__id = $_REQUEST['ygo_monster_type__id'];
$level = $_REQUEST['level'];
$atk = $_REQUEST['atk'];
$def = $_REQUEST['def'];
$where = "1=1";
$where .= empty($card_name) ? "" : " AND c.card_name LIKE :card_name";
$where .= empty($card_number) ? "" : " AND card_number LIKE :card_number";
$where .= empty($setlist__id) ? "" : " AND c.setlist__id = :setlist__id";
$where .= empty($ygo_rarity__id) ? "" : " AND c.ygo_rarity__id = :ygo_rarity__id";
$where .= empty($ygo_card_type__id) ? "" : " AND c.ygo_card_type__id = :ygo_card_type__id";
$where .= empty($ygo_attribute__id) ? "" : " AND c.ygo_attribute__id = :ygo_attribute__id";
$where .= empty($ygo_monster_type__id) ? "" : " AND c.ygo_monster_type__id = :ygo_monster_type__id";
$where .= empty($level) ? "" : " AND c.level = :level";
$where .= empty($atk) ? "" : " AND c.atk = :atk";
$where .= empty($def) ? "" : " AND c.def = :def";
$q = "SELECT c.id, CONCAT_WS('-', s.set_abbr, c.card_number) as card_number, c.card_name, c.ygo_rarity__id, a.card_attribute, ct.card_type, m.type, c.level, c.atk, c.def FROM priceguide.cardlist c
INNER JOIN priceguide.setlist s
ON c.setlist__id = s.id
LEFT JOIN priceguide.ygo_attribute a
ON c.ygo_attribute__id = a.id
LEFT JOIN priceguide.ygo_card_type ct
ON c.ygo_card_type__id = ct.id
LEFT JOIN priceguide.ygo_monster_type m
ON c.ygo_monster_type__id = m.id
WHERE $where
ORDER BY card_number ASC";
$stmt = $CONN->prepare($q);
$card_name = "%".$card_name."%";
$card_number = "%".$card_number."%";
if(!empty($card_name)) $stmt->bindValue(":card_name", $card_name, PDO::PARAM_STR);
if(!empty($card_number)) $stmt->bindValue(":card_number", $card_number, PDO::PARAM_STR);
if(!empty($setlist__id)) $stmt->bindValue(":setlist__id", $setlist__id, PDO::PARAM_INT);
if(!empty($ygo_rarity__id)) $stmt->bindValue(":ygo_rarity__id", $ygo_rarity__id, PDO::PARAM_INT);
if(!empty($ygo_card_type__id)) $stmt->bindValue(":ygo_card_type__id", $ygo_card_type__id, PDO::PARAM_INT);
if(!empty($ygo_attribute__id)) $stmt->bindValue(":ygo_attribute__id", $ygo_attribute__id, PDO::PARAM_INT);
if(!empty($ygo_monster_type__id)) $stmt->bindValue(":ygo_monster_type__id", $ygo_monster_type__id, PDO::PARAM_INT);
if(!empty($level)) $stmt->bindValue(":level", $level, PDO::PARAM_INT);
if(!empty($atk)) $stmt->bindValue(":atk", $atk, PDO::PARAM_INT);
if(!empty($def)) $stmt->bindValue(":def", $def, PDO::PARAM_INT);
$stmt->execute();
我很确定我现在已经用一把精细的齿梳梳理了所有东西,而且一切似乎都是正确的。使用if
语句并不重要,因为如果变量未设置,则不会有任何参数可供其附加。所以我对这件事感到困惑。为什么它告诉我,我的参数不正确时,他们不是?
更新:根据请求,我添加了javascript代码以显示我的数据来自哪里。
答案 0 :(得分:1)
我猜是
$card_name = "%".$card_name."%";
$card_number = "%".$card_number."%";
if(!empty($card_name)) $stmt->bindValue(":card_name", $card_name, PDO::PARAM_STR);
if(!empty($card_number)) $stmt->bindValue(":card_number", $card_number, PDO::PARAM_STR);
导致问题 - $card_name
和$card_number
此时永远不会为空,即使它们在您创建$where
时为空,因此您将拥有太多的绑定变量。