2012-08-14 64 views
2

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

?> 
+0

如果从插入和删除ID会发生什么情况的结合,即参数列表,因为它是一个auto_incrementing字段? – TommyBs 2012-08-14 06:39:19

+0

@TommyBs当我拿出自动递增字段时,我仍然得到相同的错误 – cadavid4j 2012-08-14 06:40:07

+0

对不起,我的第一个评论我正在考虑PDO而不是mysqli,因此我删除了它。我不确定你是否需要在bind_param部分中的变量的引号。但是,如果您删除$ id,如上所述,会发生什么 – TommyBs 2012-08-14 06:49:21

回答

1

你应该看看相应的mysqli_stmt::bind_param文档。更确切地说,看看功能的定义:

bool mysqli_stmt::bind_param (string $types , mixed &$var1 [, mixed &$... ]) 

请注意mixed &$var1部分?这基本上说明你的参数是通过引用传递的,而不是通过值(这看起来像mixed $var1 - &有所作为)。

现在,您调用的问题是您正试图传递表达式而不是通过引用的变量。从PHP documentation

The following things can be passed by reference:
- Variables, i.e. foo($a)
- New statements, i.e. foo(new foobar())
- References returned from functions, [...]

简单的补救方法是首先调用的是未初始化变量,然后将其分配给您处理的输入数据

// Prepared statement for a query to place something in the database 
$stmt = $db->prepare("insert into pages (id, title, body, date) values (?,?,?,?)"); 

if (!$stmt) { 
    echo "Prepare failed: (" .$db->errno . ")" . $db->error; 
} 

if (!$stmt->bind_param('isss', $stmt_id, $stmt_title, $stmt_body, $stmt_date)) { 
    echo "Binding parameters failed: (" .$stmt->errno. ")" . $stmt->error; 
} 

$stmt_id = (int) $id; 
$stmt_title = (string) $title; 
$stmt_body = (string) $body; 
$stmt_date = (string) $date; 

if (!$stmt->execute()) { 
    echo "Execute failed: (" .$stmt->errno . ") " .$stmt->error; 
} 
+0

对于这样的详细和很好的回应,真的不能够感谢你。它工作正常,我学到了我想学的东西。非常感谢你。 – cadavid4j 2012-08-14 07:27:52

+0

很高兴能帮到你! – aefxx 2012-08-14 07:28:38