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