我試圖在PHP/MYSQL中學習準備好的語句,因爲這裏有很多建議。我不斷收到此錯誤:嘗試準備語句時無法傳遞參數
Fatal error: Cannot pass parameter 2 by reference in C:\xampp\htdocs\blog\admin\create.php on line 57
誰能告訴我如何解決這個問題?我一直在四處搜尋,找不到能幫助我解決問題的任何事情。
這裏是我的代碼:
<?php
require_once '../config.php';
// Check to see if the title was entered from new.php
if ($_POST['title'])
{
$title = $_POST['title'];
} else {
echo "No title was entered. Please go back. <br />";
}
// Check to see if the body was entered from new.php
if ($_POST['body'])
{
$body = $_POST['body'];
} else {
echo "No body was entered. Please go back. <br />";
}
// Get the date
$date = time();
// ID = NULL because of auto-increment
$id = 'NULL';
// If magic_quotes_gpc returns true then it's enabled on the serever and all variables will be
// automatically escaped with slashes. If it isn't true then it's done manually
if (!get_magic_quotes_gpc())
{
$title = addslashes($title);
$body = addslashes($body);
$date = addslashes($date);
}
// Connect to the database
$db = new mysqli('localhost','username','password','database');
// Check to see if the connection works
if ($db->connect_errno)
{
echo 'Error: Could not connect to database. Please try again.';
exit;
}
// Prepared statement for a query to place something in the database
if(!($stmt = $db->prepare("insert into pages (id, title, body, date) values (?,?,?,?)")))
{
echo "Prepare failed: (" .$db->errno . ")" . $db->error;
}
// THIS IS THE LINE WHERE I'M RECEIVING THE ERROR!!!!!!!!
if (!$stmt->bind_param('isss', ''.$id.'', ''.$title.'',''.$body.'',''.$date.''))
{
echo "Binding parameters failed: (" .$stmt->errno. ")" . $stmt->error;
}
if (!$stmt->execute())
{
echo "Execute failed: (" .$stmt->errno . ") " .$stmt->error;
}
$db->close;
?>
如果從插入和刪除ID會發生什麼情況的結合,即參數列表,因爲它是一個auto_incrementing字段? – TommyBs 2012-08-14 06:39:19
@TommyBs當我拿出自動遞增字段時,我仍然得到相同的錯誤 – cadavid4j 2012-08-14 06:40:07
對不起,我的第一個評論我正在考慮PDO而不是mysqli,因此我刪除了它。我不確定你是否需要在bind_param部分中的變量的引號。但是,如果您刪除$ id,如上所述,會發生什麼 – TommyBs 2012-08-14 06:49:21