0

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>&nbsp;</td></tr>';
    echo '<tr>';
    
    echo '<td>&nbsp;</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);
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
uddin124
  • 1
  • 1
  • Hi! Please stick to one problem per post, and limit the code you show to a [mcve] that demonstrates that particular problem. Remember to include all relevant input, the expected behaviour or output, and the exact actual behaviour or output, including the full text of any error messages. See the [help], particularly [ask] for more tips on helping us to help you. – IMSoP Apr 16 '22 at 08:20
  • hi, sorry. i thought it would be best to include the problems i am facing with it as it might be related/correlated to the inital problem. – uddin124 Apr 16 '22 at 08:24
  • Yes, the bigger problem is that there's just too much code here, and not enough detail of what's wrong. You mention an error message, but don't quote it in full; among other things, it will tell you which line it's happening on, allowing you to focus on that part of the code. You can then see which parts of the code you can remove or simplify and still see the error - for instance, temporarily replacing the database results with a hard-coded array, perhaps generated using var_export from the real thing. – IMSoP Apr 16 '22 at 08:29
  • You might also be able to work out what the error message is trying to tell you from this reference page: https://stackoverflow.com/q/12769982/157957 – IMSoP Apr 16 '22 at 08:31
  • All those `echoes` of html, makes managing string enclosures more buggy. You can `?>` to insert the html and then use `=` or ` – mardubbles Apr 16 '22 at 08:45
  • Hi, I have edited my question. is this better? I figured out what happens but I don't know how to solve it. – uddin124 Apr 16 '22 at 08:46
  • Debugging poorly written code is likely harder on a third party (me), when you can use best practices in the first place, which you dont. – mardubbles Apr 16 '22 at 08:49
  • @mardubbles yes, i have now edited the question. my problem is to do with the php. im using basic html in order to not get too mixed up in things. – uddin124 Apr 16 '22 at 08:50
  • What is this line supposed to do for example, `if ($results2>null)`? Is "null" suddenly enumerable? I get it you dont know what you are doing, but making up rules kinda goes against of using a "language". – mardubbles Apr 16 '22 at 08:50
  • @mardubbles, no it is simply to do the statement. to get it going. it worked for me in the past – uddin124 Apr 16 '22 at 08:51
  • "it worked for me in the past" but not now? Yeah fixing errors without understanding what you write, will lead to logical errors in future (now). – mardubbles Apr 16 '22 at 08:55
  • So you should `var_dump($_SESSION["cart_products"])` to _know your data_, instead of suspecting that a null value may be added. You assign `$new_product` to your session _outside_ the `while` loop, and given your `$results2>null` statement, that assignment will be executed even when the `while` iterates 0 times. And therefore you end up with a cart item without the necessary keys fetched from your database. – Markus AO Apr 16 '22 at 09:37
  • On your `if ($results2>null)` statement: `mysqli_query` will only return `false` if a query _fails_ (is invalid etc.), otherwise you get `true` (even if zero rows returned) or an object (when result set is present). These are coerced to `(int) 1` for `>` comparison. `null` and `false` are coerced to `(int) 0`. Then, you're comparing `(object) | true | false` is greater than `null`, and that comparison will be true for any valid queries. On a general note, comparisons that involve type coercion are prone to bugs. Aim to write _code that makes sense_, don't rely on _magically seems to work_. – Markus AO Apr 16 '22 at 09:45
  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Apr 16 '22 at 10:23

0 Answers0