我试图在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