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

바인딩 호출하는 것입니다 param list는 auto_incrementing 필드이므로? – TommyBs

+0

@ TommyBs 자동 증가 필드를 꺼낼 때 나는 여전히 같은 오류가 발생합니다. – cadavid4j

+0

미안 해요. 첫 번째 의견은 PDO가 아닌, mysqli가 아니라고 생각해서 죄송합니다. bind_param 부분에서 변수 주위에 따옴표가 필요하지는 않습니다. 그러나 위에서 설명한대로 $ id를 제거하면 어떻게됩니까? – TommyBs

답변

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, [...]

간단한 치료는 먼저 즉, 후 처리 된 입력 데이터를 할당 초기화되지 않은 변수는 삽입과에서 ID를 제거하면 어떻게됩니까

// 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

+0

기꺼이 도와 드리겠습니다! – aefxx

관련 문제