Php 致命错误:未捕获异常“exception”,消息“id未提供”
完整错误如下:致命错误:未捕获异常“exception”,在第12+行的/var/www/html/pdo/Delete.php中显示消息“未提供id”!异常:第12行的/var/www/html/pdo/Delete.php中未提供id。我正在测试一个页面,以调用删除函数,该函数删除与我在测试中给出的id匹配的行。它工作了,它删除了具有该id的行。但是给了我这个警告。这是我的密码:Php 致命错误:未捕获异常“exception”,消息“id未提供”,php,mysql,pdo,Php,Mysql,Pdo,完整错误如下:致命错误:未捕获异常“exception”,在第12+行的/var/www/html/pdo/Delete.php中显示消息“未提供id”!异常:第12行的/var/www/html/pdo/Delete.php中未提供id。我正在测试一个页面,以调用删除函数,该函数删除与我在测试中给出的id匹配的行。它工作了,它删除了具有该id的行。但是给了我这个警告。这是我的密码: <?php include_once("StudentManager.php"); //
<?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
此行抛出导致您看到的致命错误的异常
它在以下条件下运行:
if(!isset($id)){
显然,条件匹配,这意味着$id变量没有设置
此外,提取$\请求是非常糟糕的做法
简单范围示例:
function foo($a) {
$a = 5;
echo $a; //5
}
$a = 42;
echo $a; //42
foo($a); //will echo 5
echo $a; //Still 42. Different $a.
请不要将代码转储到注释中。编辑我们的原始帖子以添加代码。@Jay Blanchard,谢谢,我删除了我的评论并将其添加到代码中。你知道准备好的语句是什么吗?如果是,为什么不使用预先准备好的语句绑定id?@N.B.,是的,你是对的,我做得更好,但它仍然给我一个例外。它仍然有效,就像从数据库中删除它一样,但它给我带来了致命的打击。但我认为我的检查是看id是否没有传入,它是否在我的测试中。所以!isset应该忽略它,因为它已设置。或者我看错了。为什么它会检查这样的东西?传递给isset的$id和传递给函数的$id是不同的。阅读PHP中作用域的工作原理其实并没有那么复杂。是的,就是作用域。我在错误的地方测试代码。它现在工作得很好,我正在其他地方测试它。
function foo($a) {
$a = 5;
echo $a; //5
}
$a = 42;
echo $a; //42
foo($a); //will echo 5
echo $a; //Still 42. Different $a.