php中的isset函数通知

php中的isset函数通知

问题描述:

The code below throws a notice.

( ! ) Notice: Undefined index: product_id in C:\xampp\htdocs\WilliesFishing\admin\product\index.php on line 55

I checked and I am on line 55 but it doesn't like the isset().

I have also tried if(isset($_GET['product_id']; and it throws the same error. Shouldn't this work?

case 'show_add_edit_form':
    if (isset($product_id)) {
        $product_id = $_GET['product_id'];
    } else {  
        $product_id = $_POST['product_id']; // --> Line #55
    }

Either $_GET or $_POST doesn't have an index named product_id. Your isset line only checks whether the global variable $product_id is set, which may not imply anything about the state of $_GET and/or $_POST. You probably want to do something like this instead:

if (isset($_GET['product_id')) {
    $product_id = $_GET['product_id'];
} else if (isset($_POST['product_id')) {
    $product_id = $_POST['product_id'];
} else {
    // provide a reasonable default, or otherwise handle the edge-case, in here
}

You should use isset on your get parameters. Something like this:

 if (isset($_GET['product_id'])) {
     $product_id = $_GET['product_id'];
 }
 else if (isset($_POST['product_id'])) {  
     $product_id = $_POST['product_id'];
 }

The error tells you this because $_GET['product_id'] has no value. So instead of using $product_id. use the request parameter in the condition instead