2011-09-01 75 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

:新的是另一个:)谢谢 –