【发布时间】:2015-02-08 22:38:51
【问题描述】:
我正在尝试更新我的网站以使用准备好的语句,但我不断收到此错误,我似乎无法弄清楚原因。我已经在 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;
此代码输出以下内容:
Preparing query...(0)
Binding params...(0)
Executing statement...(2031) No data supplied for parameters in prepared statement
很明显,没有任何东西被插入到数据库中。请帮助我理解我做错了什么。提前谢谢大家。
埃里克
【问题讨论】:
-
首先为变量设置三元运算符,然后
bind_param()一次,正如 Barmar 在他的回答中所说的那样。
标签: php mysql mysqli runtime-error prepared-statement