title: PHP undefined array for website shopping cart
body: <p>i have found out that when i press the update button, regardless of doing anything, it comes up with this message:</p>
  <blockquote>
  <p>Warning: Undefined array key &quot;Product_Name&quot; in C:\xampp\htdocs\website\bootsp.php on line 35
  <br>
  Warning: Undefined array key &quot;product_qty&quot; in C:\xampp\htdocs\website\bootsp.php on line 36
  <br>
  Warning: Undefined array key &quot;Price&quot; in C:\xampp\htdocs\website\bootsp.php on line 37
  <br>
  Warning: Undefined array key &quot;Product_ID&quot; in C:\xampp\htdocs\website\bootsp.php on line 38
  array(1) { [1]=&gt; string(1) &quot;1&quot; }</p>
  </blockquote>
  <p>what i suspect is happening, is that when i press the update button regardless of pressing the remove checklist is that it automatically adds a null variable to the $ variables which messes up my code, i am not sure why this happens.</p>
  <p>boots page php code for the shopping list:</p>
  <pre><code>&lt;?php
  if(isset($_SESSION[&quot;cart_products&quot;]) &amp;&amp; count($_SESSION[&quot;cart_products&quot;])&gt;0)
  {
      
      echo '&lt;h3&gt;Your Shopping Cart&lt;/h3&gt;';
      echo '&lt;form method=&quot;post&quot; action=&quot;cart_update.php&quot;&gt;';
      echo '&lt;table width=&quot;100%&quot;  cellpadding=&quot;6&quot; cellspacing=&quot;0&quot;&gt;';
      echo '&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Quantity&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Remove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;';
      echo '&lt;tbody&gt;';
  
  
      $total =0;
      $b = 0;
  
      foreach ($_SESSION[&quot;cart_products&quot;] as $cart_itm)
      {
          $Product_Name = $cart_itm[&quot;Product_Name&quot;];
          $product_qty = $cart_itm[&quot;product_qty&quot;];
          $Price = $cart_itm[&quot;Price&quot;];
          $Product_ID = $cart_itm[&quot;Product_ID&quot;];
          
          $bg_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe
  
          echo '&lt;tr class=&quot;'.$bg_color.'&quot;&gt;';
          echo '&lt;td&gt;&lt;input type=&quot;text&quot; size=&quot;2&quot; maxlength=&quot;2&quot; name=&quot;product_qty['.$Product_ID.']&quot; value=&quot;'.$product_qty.'&quot; /&gt;&lt;/td&gt;';
          echo '&lt;td&gt;'.$Product_Name.'&lt;/td&gt;';
          echo '&lt;td&gt;&lt;input type=&quot;checkbox&quot; name=&quot;remove_code[]&quot; value=&quot;'.$Product_ID.'&quot; /&gt; Remove&lt;/td&gt;';
          echo '&lt;/tr&gt;';
          $subtotal = ($Price * $product_qty);
          $total = ($total + $subtotal);
          var_dump($cart_itm);
          //unset($_SESSION[&quot;cart_products&quot;]);
      }
  
      echo '&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;';
      echo '&lt;tr&gt;';
      
      echo '&lt;td&gt;&amp;nbsp;&lt;/td&gt;';
      echo '&lt;td&gt;';
      echo '&lt;button type=&quot;submit&quot; id=&quot;myButton&quot;&gt;Update&lt;/button&gt;&lt;/td&gt;';
  
  
      echo '&lt;td&gt;&lt;a href=&quot;view_cart.php&quot; id=&quot;myButton&quot; style=&quot;width: 18%; padding-left: 8px;&quot;&gt;Checkout&lt;/a&gt;';
      echo '&lt;/td&gt;';
      echo '&lt;/tr&gt;';
      echo '&lt;/tbody&gt;';
      echo '&lt;/table&gt;';
      
      $current_url = urlencode($url=&quot;http://&quot;.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
      echo '&lt;input type=&quot;hidden&quot; name=&quot;return_url&quot; value=&quot;'.$current_url.'&quot; /&gt;';
      echo '&lt;/form&gt;';
  }
  ?&gt;
  </code></pre>
  <p>my cart update php file code:</p>
  <pre><code>&lt;?php
  include 'config.php';
  session_start();
  if(isset($_POST[&quot;type&quot;]) &amp;&amp; $_POST[&quot;type&quot;]=='add'){
      foreach($_POST as $key =&gt; $value){ //add all post vars to new_product array
          $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
      }
      //remove unecessary vars
      unset($new_product['type']);
      unset($new_product['return_url']);
      //$temp_prod_id = $new_product['Product_ID'];
      $results = &quot;select Product_ID, Product_Name, Product_Img_name, Category, Price, Description FROM products where Product_ID = '&quot;.$new_product['Product_ID'].&quot;'&quot;;
      $results2 = mysqli_query($con,$results);
      if ($results2&gt;null){
          while ($obj = mysqli_fetch_row($results2)) {
              //$Product_ID = $new_product['Product_ID'];
              $new_product['Product_ID']=$obj[0];
              $new_product['Product_Name']=$obj[1];
              $new_product['Product_Img_name']=$obj[2];
              $new_product['Price']=$obj[4];
  
              if(isset($_SESSION[&quot;cart_products&quot;])){  //if session var already exist
                  if(isset($_SESSION[&quot;cart_products&quot;][$new_product['Product_ID']])) //check item exist in products array
                  {
                      unset($_SESSION[&quot;cart_products&quot;][$new_product['Product_ID']]); //unset old array item
                  }           
              }
              $_SESSION[&quot;cart_products&quot;][$new_product['Product_ID']] = $new_product; //update or create product session with new item
          }
      }
  }
  
  
  if(isset($_POST[&quot;product_qty&quot;]) || isset($_POST[&quot;remove_code&quot;])){
      //update item quantity in product session
      if(isset($_POST[&quot;product_qty&quot;]) &amp;&amp; is_array($_POST[&quot;product_qty&quot;])){
          foreach($_POST[&quot;product_qty&quot;] as $key =&gt; $value){
              if(is_numeric($value)){
                  $_SESSION[&quot;cart_products&quot;][&quot;product_qty&quot;][$key] = $value;
              }
          }
      }
      //remove an item from product session
      if(isset($_POST[&quot;remove_code&quot;]) &amp;&amp; is_array($_POST[&quot;remove_code&quot;])){
          foreach($_POST[&quot;remove_code&quot;] as $key){
              unset($_SESSION[&quot;cart_products&quot;][$key]);
          }   
      }
  }
  
  $return_url = (isset($_POST[&quot;return_url&quot;]))?urldecode($_POST[&quot;return_url&quot;]):''; //return url
  header('Location:'.$return_url);
  ?&gt;
  </code></pre>