0

I am getting an undefined index notice in my web app. I can not figure out where I am going wrong. If I submit nothing it goes to the error page and gives the second error below.

First Undefined index

 Notice: Undefined index: product_id in C:\xampp\htdocs\book_apps\ch24_guitar_shop\admin\product\index.php on line 56

Undefined variable

 Notice: Undefined variable: error_message in C:\xampp\htdocs\book_apps\ch24_guitar_shop\errors\error.php on line 5

Index page associated with undefined index

<?php
require_once('../../util/main.php');
require_once('util/secure_conn.php');
require_once('util/valid_admin.php');
require_once('util/images.php');
require_once('model/product_db.php');
require_once('model/category_db.php');




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

$action = strtolower($action);
switch ($action) {
    case 'list_products':
        // get categories and products
        if(isset($_GET['category_id'])){
        $category_id = $_GET['category_id'];
        }
        if (empty($category_id)) {
            $category_id = 1;
        }
        $current_category = get_category($category_id);
        $categories = get_categories();
        $products = get_products_by_category($category_id);

        // display product list
        include('product_list.php');
        break;
    case 'view_product':
        $categories = get_categories();
        $product_id = $_GET['product_id'];
        $product = get_product($product_id);
        $product_order_count = get_product_order_count($product_id);
        include('product_view.php');
        break;
    case 'delete_product':
        $category_id = $_POST['category_id'];
        $product_id = $_POST['product_id'];
        delete_product($product_id);

        // Display the Product List page for the current category
        header("Location: .?category_id=$category_id");
        break;
    case 'show_add_edit_form':
        if (isset($_GET['product_id'])) {
            $product_id = $_GET['product_id'];
        } else {

      /*Line 56*/      $product_id = $_POST['product_id'];

        }
        $product = get_product($product_id);
        $categories = get_categories();
        include('product_add_edit.php');
        break;
    case 'add_product':
        $category_id = $_POST['category_id'];
        $code = $_POST['code'];
        $name = $_POST['name'];
        $description = $_POST['description'];
        $price = $_POST['price'];
        $discount_percent = $_POST['discount_percent'];

        // Validate inputs
        if (empty($code) || empty($name) || empty($description) ||
            empty($price) ) {
            $error = 'Invalid product data.
                      Check all fields and try again.';
            include('../../errors/error.php');
        } else {
            $categories = get_categories();
            $product_id = add_product($category_id, $code, $name,
                    $description, $price, $discount_percent);
            $product = get_product($product_id);
            include('product_view.php');
        }
        break;
    case 'update_product':
        $product_id = $_POST['product_id'];
        $code = $_POST['code'];
        $name = $_POST['name'];
        $description = $_POST['description'];
        $price = $_POST['price'];
        $discount_percent = $_POST['discount_percent'];
        $category_id = $_POST['category_id'];

        // Validate inputs
        if (empty($code) || empty($name) || empty($description) ||
            empty($price) ) {
            $error = 'Invalid product data.
                      Check all fields and try again.';
            include('../../errors/error.php');
        } else {
            $categories = get_categories();
            update_product($product_id, $code, $name, $description,
                           $price, $discount_percent, $category_id);
            $product = get_product($product_id);
            include('product_view.php');
        }
        break;
    case 'upload_image':
        $product_id = $_POST['product_id'];
        $product = get_product($product_id);
        $product_code = $product['productCode'];

        $image_filename = $product_code . '.png';
        $image_dir = $doc_root . $app_path . $image_dir . 'images/';

        if (isset($_FILES['file1'])) {
            $source = $_FILES['file1']['tmp_name'];
            $target = $image_dir . DIRECTORY_SEPARATOR . $image_filename;

            // save uploaded file with correct filename
            move_uploaded_file($source, $target);

            // add code that creates the medium and small versions of the image
            process_image($image_dir, $image_filename);

            // display product with new image
            include('product_view.php');
        }
        break;
}
?>

Error Page

<?php include 'view/header.php'; ?>
<div id="content">
    <h2>Error</h2>

    <p><?php echo $error_message; ?></p>
</div>
<?php include 'view/footer.php'; ?>
  • 3
    It'd be helpful if you actually pointed out WHERE line 56 is. And the error tells you EXACTLY what the problem is anyways. You're accessing array elements that don't exist and using variables that haven't been defined. – Marc B Apr 28 '14 at 03:13
  • Sorry about that. I added where line 56 is. So how could I go about fixing this. – user3562189 Apr 28 '14 at 03:16
  • 2
    Simple: don't use undefined vars and don't use undefined array keys. – Marc B Apr 28 '14 at 03:16
  • But how would I go about defining the array keys in this particular situation – user3562189 Apr 28 '14 at 03:18

1 Answers1

0

Problem:

  1. Your problem is the action show_add_edit_form, AND I guess this action is getting the product information and display as form.
  2. You using same action for A) display form for new product, B) display form with existing product information.

Fix:

  1. Using different action name for these 2 actions, OR
  2. try using following code to prevent getting the product information

Code:

case 'show_add_edit_form':
    if (isset($_GET['product_id'])) {
        $product = get_product( $_GET['product_id'] );
    } elseif(isset($_POST['product_id'])) {
        $product = get_product( $_POST['product_id'] );
    }

    $categories = get_categories();
    include('product_add_edit.php');
    break;
Allen Chak
  • 1,802
  • 1
  • 10
  • 21