2011-09-01 70 views
1

我想從普通的mysql查詢更改爲PDO,但從那時起我甚至無法追溯故障。php提交PDO SQL問題

有沒有人幫助我?任何提示將不勝感激!

問題1:有什麼問題? Q2:一般如何追蹤故障?

<?php 
// Check if add button active, start this 
if (isset($_POST['create'])) { 
$new=htmlspecialchars($_POST['newgroup']); 
$sql = "INSERT INTO contactgroups (status, gr_name) VALUES(1, : new)"; 
$sql->bindvalue(': new', $new); 
$sql->execute(); 
$arr = $sql->errorInfo(); 
echo $arr; 
echo "<meta http-equiv=\"refresh\" content=\"0;URL=groups.php\">"; 
} 
?> 

<form id="group-in" method="post" action="groups.php"> 
Add new Distribution group: <input type="text" name="newgroup" placeholder="name..."> <input type="submit" name="create" value="Create new"> 
Rename groupname: <input type="text" value="<?php echo $result['gr_name']; ?>"> <input type="submit" name="rename" value="Rename"> 
</form> 

的$ _ POST快到了,SQL正在運行,但bindPARAM/bindVALUE沒有東西后...

+0

+1 @JürgenThelen:那是另一個錯!非常感謝。 –

回答

1

你從來沒有準備你上面的SQL語句。

爲了運行,您首先需要初始化數據庫並創建一個新的PDO實例。假設你有這樣做之前:

<?php 
// Check if add button active, start this 
if (isset($_POST['create'])) { 
$new=htmlspecialchars($_POST['newgroup']); 
$sql = "INSERT INTO contactgroups (status, gr_name) VALUES(1, : new)"; 
$stmt = $db->prepare($sql); 
$stmt->bindvalue(': new', $new); 
$stmt->execute(); 
$arr = $stmt->errorInfo(); 
echo $arr; 
echo "<meta http-equiv=\"refresh\" content=\"0;URL=groups.php\">"; 
} 
?> 

你可以用一個函數/位代碼用try/catch語句,並啓用異常與PDO處理,以獲得更好的洞察錯誤向前發展。

+0

感謝準備肯定是錯誤之一。現在我有42000錯誤碼?沒有谷歌的結果? –

+0

:新的是另一個:)謝謝 –