2012-08-14 63 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