-1

I'm new to PHP and I've read many of the other undefined index error solutions on stackoverflow, but I still can't figure out why this rather simple code is receiving it. There's data in the database and everything else is pulling ok.

Right now I'm just trying to make sure the function and query are flowing through properly. So I purposely don't have if statements to check and see if the items are selected on the form because I'm always selecting them during testing. I must be missing something simple. Please help and thank you!

Form:

<!DOCTYPE html>
<html>
    <head><title>Databases</title></head>
    <body>
            <h1>Music Store Database</h1>
            <form method='POST' action='display.php'
                 <label>Select a table:</label>
                    <select name="tableName">
                            <option value="products">Products</option>
                            <option value="categories">Categories</option>
                    </select>
               <p>Retrieve Record(s) - Select field(s) below:</p>
                    <input type="checkbox" name="productIDcb"/>
                    <label>ProductID</label><br />
                    <input type="checkbox" name="categoryIDcb"/>
                    <label>CategoryID</label><br />
                    <input type="checkbox" name="productCodecb"/>
                    <label>Product Code</label><br />
                    <input type="checkbox" name="productNamecb"/>
                    <label>Product Name</label><br />
                    <input type="checkbox" name="listPricecb"/>
                    <label>List Price</label><br />
                    <input type="checkbox" name="categoryNamecb"/>
                    <label>Category Name</label><br />
                    .
                    .
                    . 
             <p>Select the appropriate action based on your selection from above:</p>
                    <input type="radio" name="operation" value="retrieve"/>
                    <label>Retrieve Information</label><br />
                    <input type="radio" name="operation" value="addition"/>
                    <label>Add Information</label><br />
                    <input type="radio" name="operation" value="delete"/>
                    <label>Delete Information</label><br />
               <p><input type="submit" value="Submit Request"/></p>

display.php File

<?php
  require('database.php');
  require('product_list.php');
  $table = $_POST['tableName'];
  $operation = $_POST['operation'];
  $productIDcb = $_POST['productIDcb'];
  $categoryIDcb = $_POST['categoryIDcb'];
  $productCodecb = $_POST['productCodecb'];
  $productNamecb = $_POST['productNamecb'];
  $listPricecb = $_POST['listPricecb'];

if($operation == 'retrieve')
        {

                if($table == "products")
                {
                        include_once('product_list.php');
                        show_products($table, $productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb);
                }

        }

?>

product_list.php file

<?php
  include('database.php');

  function show_products($table, $productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb)
  {
        global $db;
        $theQuery = "select productId, categoryID, productCode, productName, listPrice ";
        $theQuery .=" from ". $table;
        echo($theQuery);
        $rSet = $db -> query($theQuery);
        $list = "";
        foreach($rSet AS $products)
          {
              $list .= "".$products['productID']
                      . "".$products['categoryID']
                      . "".$products['productCode']
                      . "".$products['productName']
                      . "".$products['listPrice']
                      . "<br>";
          }
        echo($list);
  }


?>
  • the duplicate reference is just a sign of another error in this case. At least that's what my research shows. It turns out the Undefined Index was due to a typo of all things. – user3025217 Nov 23 '13 at 16:28

2 Answers2

1

it is productId not productID in your select statement.

$products['productID'] should be $products['productId'] in product_list.php.

    <?php
      include('database.php');

      function show_products($table, $productIDcb, $categoryIDcb, $productCodecb, $productNamecb, $listPricecb)
      {
            global $db;
            $theQuery = "select productId, categoryID, productCode, productName, listPrice ";
            $theQuery .=" from ". $table;
            echo($theQuery);
            $rSet = $db -> query($theQuery);
            $list = "";
            foreach($rSet AS $products)
              {
                  $list .= "".$products['productId']
                          . "".$products['categoryID']
                          . "".$products['productCode']
                          . "".$products['productName']
                          . "".$products['listPrice']
                          . "<br>";
              }
            echo($list);
      }


    ?>
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

productId and productID are considered different keys in php.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Thanks - stupid typos. I think my eyes needed a break because I had looked for those before posting this and still missed it. Thanks for finding it. – user3025217 Nov 23 '13 at 16:28