skip to Main Content

I’m trying to make a foreach function where a number in a table in phpmyadmin will increase each time value inside an array matches the "pet_id". And also this only execute whenever a submit button is pressed. This is my code in php file:

<?php
    if (isset($_POST['Submit'])) {
        if (isset($cart)) {
            // if product appeared
            foreach ($product->getData('cart') as $item):
                $cart = $product->getProduct($item['pet_id']);
                $subTotal[] = array_map(function ($item){
                    $sql = "UPDATE pet_info SET number_sold = number_sold + 1 WHERE 
                    $item == pet_info.pet_id;";

                    $query1 = mysqli_query($con,$sql);
                    while ($pet_id = mysqli_fetch_array($query1)) {
                        execute($pet_id['pet_id']); 
                    }
                }); 
            endforeach;
        }   
    }​
?>

But I spent the last 2 hours searching for a solution but what i achieve is still errors:

Fatal error: Uncaught Error: Undefined constant "​" in C:xampphtdocstestingpetsonthenetpartial_checkout.php:121 Stack trace: #0 C:xampphtdocstestingpetsonthenetuser_cart.php(7): include() #1 {main} thrown in C:xampphtdocstestingpetsonthenetpartial_checkout.php on line 121

Can someone help me please? I’d be very appreciated!!

This is the part of the code related to the cart

                <?php
                foreach ($product->getData('cart') as $item) :
                    $cart = $product->getProduct($item['pet_id']);
                    echo gettype($item);
                    $subTotal[] = array_map(function ($item){
            ?>
            <!-- cart item -->
            <div class="row border-top py-3 mt-3">
                <div class="col-sm-2">
                    <img src="<?php echo $item['image'] ?? "./images/pet (1).jpg" ?>" style="height: 150px; width:120px;" alt="cart1" class="img-fluid">
                </div>
                
                <div class="col-sm-8">
                    <h5 class="font-baloo font-size-20"><?php echo $item['name'] ?? "Unknown"; ?></h5>
                    <h5 class="font-baloo font-size-20"><?php echo $item['pet_id'] ?? "Unknown"; ?></h5>
                    <small> <?php echo $item['breed'] ?? "Brand"; ?></small>
                        
                </div>

                <div class="col-sm-2 text">
                    <div class="font-size-20 text-danger font-baloo">
                        $<span class="product_price" data-id="<?php echo $item['pet_id'] ?? '0'; ?>"><?php echo $item['price'] ?? 0; ?></span>
                    </div>
                </div>
                <form method="post">
                            <input type="hidden" value="<?php echo $item['pet_id'] ?? 0; ?>" name="pet_id">
                            <button type="submit" name="delete-cart-submit" class="btn font-baloo text-danger px-3 border-right">Delete</button>
                </form>
            </div>
            <!-- !cart item -->
            <?php
                        return $item['price'];
                    }, $cart); // closing array_map function
                endforeach;
            ?>

2

Answers


  1. I just did a check on your error message, there was a unicode character that didn’t display correctly in it, in quotes.
    So I determined this was a "typo", if I’m not mistaken you are using mac or linux, and open the Vietnamese typewriter.

    You can paste the error message into your browser’s javascript console. You will see the error character.

    Login or Signup to reply.
  2. You have a zero width space, particularly <0x200b> (invisible in a simple text editor), after the last curly brace:

    ...
                endforeach;
            }   
        }​  //<-- here
    ?>
    

    enter image description here

    This appears to be an undefined constant to PHP.

    Delete what is after that last curly brace.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search