2013-07-31 39 views
1

當我嘗試將$stmt->bindParam(':username', $username, PDO::PARAM_STR);添加到我的腳本時,出現致命錯誤。以下是我的腳本的一部分。我想要做的是讓它使用會話用戶名和id。如果我拿出username = :username$stmt->bindParam(':username', $username, PDO::PARAM_STR);它會正常工作。致命錯誤無效的參數編號:混合的名稱和位置參數

致命錯誤我越來越:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters' in /home/www/test.php:7 Stack trace: #0 /home/www/test.php(7): PDOStatement->execute() #1 {main} thrown in /home/www/test.php on line 7

$action = isset($_GET['action']) ? $_GET['action']: ""; 
if($action=='delete'){ 
$username = $_SESSION['user']['username'];<<<<<<<<I ADD THIS LINE 
$query = "DELETE FROM hostingpackage WHERE username = :username And id = ?"; 
     $stmt = $db->prepare($query); 
     $stmt->bindParam(1, $_GET['id']); 
     $stmt->bindParam(':username', $username, PDO::PARAM_STR);<<<<I ADD THIS LINE 
     $result = $stmt->execute(); 
     header("Location: test.php"); 
     die("Redirecting to test.php"); 
     } 
+1

錯誤消息 「混合命名和位置參數」 那種**拼出來給你**。請勿在一個查詢中同時使用命名(':username')和位置('?')參數。 – DCoder

+0

在查詢和使用'$ stmt-> bindParam(1,$ username);'$ stmt-> bindParam(2,$ _GET ['id']);'' –

回答

1

由於錯誤說你試圖綁定PARAM 1:username在這兩個語句。我猜混亂是使用散列符號和?

最好不要混合符號,在整個過程中使用?或在整個過程中使用像:username這樣的散列符號。

請嘗試:

$query = "DELETE FROM hostingpackage WHERE username = :username And id = :id"; 
$stmt->bindParam(':username', $username, PDO::PARAM_STR);<<<<I ADD THIS LINE 
$stmt->bindParam(':id', $_GET['id']); 
$result = $stmt->execute(); 

或本:

$query = "DELETE FROM hostingpackage WHERE username = ? And id = ?"; 
$stmt->bindParam(1, $username, PDO::PARAM_STR);<<<<I ADD THIS LINE 
$stmt->bindParam(2, $_GET['id']); 
$result = $stmt->execute(); 
相關問題