【发布时间】:2015-01-04 18:38:44
【问题描述】:
完全错误是这样的:致命错误:在第 12 行的 /var/www/html/pdo/Delete.php 中未捕获的异常“异常”和消息“未提供 id”+(!)异常:id 未在 / 中提供var/www/html/pdo/Delete.php 在第 12 行。我正在测试一个页面以调用一个删除函数,该函数删除与我在测试中给出的 id 匹配的行。它有效,它删除了具有该 ID 的行。但是给了我这个警告。这是我的代码:
<?php
include_once("StudentManager.php");
//assumption is that the following parameters are passed to this file
//$id to be deleted.
//Tester
StudentManager::Delete(6); // 6 was the id I deleted.
extract($_REQUEST);
if(!isset($id)){
throw new Exception("id not supplied");
echo "Bad"; //It does not get to this.
} //else {
//echo StudentManager::Delete($id);
//}
?>
//Delete(id) and returns the number of rows affected by the delete
public static function Delete($id){
$db = StudentManager::getPDOConnection();
$sql = "DELETE FROM people WHERE id=".$id;
$di = $db->prepare($sql);
$di->execute(array(":id"=>$id));
$affected_rows = $di->rowCount();
echo "<p>$affected_rows rows were Deleted.</p>";
$db = null;
return $affected_rows;
} //end of delete
【问题讨论】:
-
请不要将代码转储到 cmets。编辑我们的原始帖子以添加代码。
-
@Jay Blanchard,谢谢,我删除了我的评论并将其添加到代码中。
-
你知道预备语句是什么吗?如果是,为什么不使用准备好的语句绑定 id?
-
@N.B.,是的,你是对的,我把它修复得更好,但它仍然给了我例外。它仍然有效,就像从数据库中删除它一样,但它对我来说是致命的。