使用MySQLi在PHP中编写的语句,获取;没有为准备好的报表中的参数提供数据”;

使用MySQLi在PHP中编写的语句,获取;没有为准备好的报表中的参数提供数据”;,php,mysql,mysqli,runtime-error,prepared-statement,Php,Mysql,Mysqli,Runtime Error,Prepared Statement,我正在尝试更新我的站点以使用预先准备好的语句,但我一直收到这个错误,我似乎无法找出原因。我搜索Google和Stackoverflow已经一周了,尝试了我找到的所有东西,但是没有任何东西能解决这个问题。我肯定我只是在某个地方误解了什么。以下是产生错误的代码: $query = "INSERT INTO `$table` (type, name, company, amount, currentbalance, interest, startingbalance, term, frequency,

我正在尝试更新我的站点以使用预先准备好的语句,但我一直收到这个错误,我似乎无法找出原因。我搜索Google和Stackoverflow已经一周了,尝试了我找到的所有东西,但是没有任何东西能解决这个问题。我肯定我只是在某个地方误解了什么。以下是产生错误的代码:

$query = "INSERT INTO `$table` (type, name, company, amount, currentbalance, interest, startingbalance, term, frequency, entrymonth, entryyear, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

echo "Preparing query...";
$addstmt = $db->prepare($query);
echo "(" . $addstmt->errno . ") " . $addstmt->error;
echo "<br>Binding params...";
$addstmt->bind_param('s', empty($type) ? "income" : $type);
$addstmt->bind_param('s', empty($name) ? "" : $name);
$addstmt->bind_param('s', empty($company) ? "" : $company);
$addstmt->bind_param('d', empty($amount) ? 0.0 : $amount);
$addstmt->bind_param('d', empty($currentbalance) ? 0.0 : $currentbalance);
$addstmt->bind_param('d', empty($interest) ? 0.0 : $interest);
$addstmt->bind_param('d', empty($startingbalance) ? 0.0 : $startingbalance);
$addstmt->bind_param('i', empty($term) ? 0 : $term);
$addstmt->bind_param('i', empty($freq) ? 4 : $freq);
$addstmt->bind_param('i', empty($month) ? 0 : $month);
$addstmt->bind_param('i', empty($year) ? 2015 : $year);
$addstmt->bind_param('s', empty($notes) ? "" : $notes);
echo "(" . $addstmt->errno . ") " . $addstmt->error;
echo "<br>Executing statement...";
$result = $addstmt->execute();
echo "(" . $addstmt->errno . ") " . $addstmt->error;
显然,数据库中没有插入任何内容。请帮助我理解我做错了什么。提前谢谢大家


Eric

您不会对每个参数重复调用
bind_param
,而是对所有参数调用一次

$addstmt->bind_param('sssddddiiiis', $type, $name, $company, $amount, $currentbalance, $interest, $startingbalance, $term, $freq, $month, $year, $notes);
您也不能在参数中使用表达式。参数绑定到引用,因此必须提供变量。要提供默认值,您必须通过设置变量本身来实现,例如

if (empty($type)) {
    $type = "income";
}

首先为变量设置三元运算符,然后像Barmar在回答中所说的那样,
bind_param()
一次。这很完美,谢谢。然而,这带来了另一个问题:假设参数的数量是动态的。(例如,表中的列数并不总是相同)在这种情况下,我如何使准备好的语句起作用?请参阅
if (empty($type)) {
    $type = "income";
}