致命错误:未捕获的异常“异常”,消息“未提供ID”

时间:2014-11-07 21:19:04

标签: php mysql pdo

完整错误是这样的:致命错误:第12行上的/var/www/html/pdo/Delete.php中带有'id not provided'消息的未捕获异常'异常'+(!)例外:/中未提供id第12行的var / www / html / pdo / Delete.php我正在测试一个页面来调用一个删除函数,删除与我在测试中给出的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

1 个答案:

答案 0 :(得分:1)

throw new Exception("id not supplied");

此行会抛出导致您看到的致命错误的异常。

它在这种情况下运行:

if(!isset($id)){

很明显,条件匹配,这意味着未设置$id变量。

此外,extract($_REQUEST)是非常糟糕的做法。

简单范围示例:

function foo($a) {
    $a = 5;
    echo $a; //5
}
$a = 42;
echo $a; //42
foo($a); //will echo 5
echo $a; //Still 42. Different $a.