1

PHP is giving me error of

Notice: Undefined index: product_id in C:\xampp\htdocs\odieinventory\admin\add_sales.php on line 6

This is my query

<?php 
  if (isset($_POST['addCart'])) {
    $product_id = $_POST['product_id'];
    $qtyBuy = $_POST['qtyBuy'];

    $addQuery = "INSERT INTO sales (product_id, quantity) VALUES ($product_id, $qtyBuy)";
    $execQuery = mysqli_query($connection, $addQuery);
  }
?>

This is my table

<form action="add_sales.php" method="POST">
        <?php 

          $query = "SELECT * FROM products";
          $exec = mysqli_query($connection, $query);

          while ($row = mysqli_fetch_array($exec)) {
            $product_id = $row['product_id'];
            $product_name = $row['product_name'];
            $description = $row['description'];
            $product_quantity = $row['quantity'];
            $product_price = $row['sell_price'];

         ?>
            <tr>
                <td class="text-center"><?php echo $product_id; ?>
                  <input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
                </td>
                <td><?php echo $product_name; ?></td>
                <td><?php echo $description; ?></td>
                <td><?php echo $product_price; ?></td>
                <td><?php echo $product_quantity; ?></td>

                <td><input type="number" min="1" max="999" name="qtyBuy"></td>
            </tr>
         <?php } ?>

        </tbody>
      </table>
      </div>
      <div class="form-group">

          <input type="submit" name="addCart" value="Add Items to Cart" class="btn btn-info pull-right">

      </div>
   </div>
</form>

My Order Page

I would also like to ask for advice on how to insert records on my cart. By clicking 'Add Items to Cart', I like to pass ONLY the items with values on the quantitiy input on the cart. How to achieve this with PHP?

Phil
  • 157,677
  • 23
  • 242
  • 245
Odie
  • 375
  • 4
  • 16
  • 1
    If you inspect the hidden element, do you actually see an id in the value? – Hanlet Escaño Aug 25 '16 at 07:17
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Justinas Aug 25 '16 at 07:18
  • For multiple rows, you will clobber the form data `product_id` with each iteration. It will only ever submit the last one – Phil Aug 25 '16 at 07:19

2 Answers2

1

First of all, you should never put users input ($_POST) directly into the SQL to avoid SQL injection - use prepared statements with mysqli.

It would be easiest for you to add a separate form with product ID and quantity field and an "Add to cart" button for each product in the table.

  • I'll read the links you've given. What do you mean on a seperate form? Can you explain further? – Odie Aug 25 '16 at 11:50
  • Well you wold iterate over the rows and inside each `` you would have a `
    ` for each product. It is not a perfect solution, but a really simple one.
    – Šimon Rozsíval Aug 27 '16 at 09:10
1

In your table file table.php , you have to:

1- open the table and tbody HTML tags

2- make your html attributes form name as array (you have more than one input with the same name), in your case you are only submiting the last product_id and the last qtyBuy input values.

table.php

<form action="add_sales.php" method="POST">
       <div><table><tbody>
        <?php

          $query = "SELECT * FROM products";
          $exec = mysqli_query($connection, $query);
          while ($row = mysqli_fetch_array($exec)) {
            $product_id = $row['product_id'];
            $product_name = $row['product_name'];
            $description = $row['description'];
            $product_quantity = $row['quantity'];
            $product_price = $row['sell_price'];

         ?>
            <tr>
                <td class="text-center"><?php echo $product_id; ?>
                  <input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
                </td>
                <td><?php echo $product_name; ?></td>
                <td><?php echo $description; ?></td>
                <td><?php echo $product_price; ?></td>
                <td><?php echo $product_quantity; ?></td>

                <td><input type="number" min="1" max="999" name="qtyBuy[]"></td>
            </tr>
            <?php } ?>

        </tbody>
      </table>
      </div>
      <div class="form-group">

          <input type="submit" name="addCart" value="Add Items to Cart" class="btn btn-info pull-right">

      </div>
</form>

In your add_sales.php file, you have to loop the $_POST['qtyBuy'] variable and check if the given quantity of each product is > 0

add_sales.php

<?php
   if (isset($_POST['addCart']) && $_POST['addCart']=="Add Items to Cart") {
     foreach($_POST['qtyBuy'] as $index=>$value){
       if($value > 0){
           $addQuery = "INSERT INTO sales (product_id, quantity) VALUES (".$_POST['product_id'][$index].", ".$value.")";
           $execQuery = mysqli_query($connection, $addQuery);
        }  
     }
  }
?>
Community
  • 1
  • 1
Amani Ben Azzouz
  • 2,477
  • 3
  • 15
  • 26
  • I can't upvote your post because I'm new here. I'm also new on PHP. Anyway, do you have any links where to learn foreach? I also saw tutorials on using sessions. Thank you. – Odie Aug 25 '16 at 11:48
  • hi you'r welcome --you can take a look here :https://www.codingunit.com/php-tutorial-foreach-loop – Amani Ben Azzouz Aug 25 '16 at 12:33