簡體   English   中英

在PHP中使用MySQLi預准備語句,得到“預准備語句中沒有為參數提供數據”

[英]Using MySQLi prepared statements in PHP, getting “No data supplied for parameters in prepared statement ”

我正在嘗試更新我的網站以使用准備好的語句,但是我一直收到此錯誤,而且我似乎無法弄清原因。 我一直在搜索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 ,而是對所有參數都調用一次。

$addstmt->bind_param('sssddddiiiis', $type, $name, $company, $amount, $currentbalance, $interest, $startingbalance, $term, $freq, $month, $year, $notes);

您也不能在參數中使用表達式。 參數綁定到引用,因此您必須提供變量。 要提供默認值,您必須自己設置變量,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM