I am trying to make a system for office staff and delivery drivers so the office staff book the orders in and the delivery drivers are able to see what can see what orders there are for that day and for their lorry as will be multiple lorries/drivers. I am currently working on the sell product order page but I am getting the following error

It was working before I added extra code in so the customer name and lorry name is populated from the customers and lorries db table and when a customer name is selected, there is a next textarea box next to it that is populated with the customers address

[30-Oct-2024 15:30:07 UTC] PHP Fatal error: Uncaught Error: Unsupported operand types in /home/wwwbeechwoodsolu/public_html/sites/coal-lorry-system-two/sell_product.php:23
Stack trace:
#0 {main}
thrown in /home/wwwbeechwoodsolu/public_html/sites/coal-lorry-system-two/sell_product.php on line 23

On line 23 is

$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];

But don't think that line is the issue as was not showing any issue with that line or the code before I added extra code in

It's bit of long file to post the whole code here so I attached it as a txt file to this post

Below is the sections of code that contains the code I put in

//Code for Checkout
if(isset($_POST['checkout'])){
    $invoiceno= mt_rand(100000000, 999999999);
    $pid=$_SESSION['productid'];
    $quantity=$_POST['quantity'];
    $lorryname=$_POST['lorryname']; // I ADDED THIS LINE IN
    $customername=$_POST['customername']; // I ADDED THIS LINE IN
    $customeraddress=$_POST['customeraddress']; // I ADDED THIS LINE IN
    $pmode=$_POST['paymentmode'];
    $value=array_combine($pid,$quantity);
    foreach($value as $pdid=> $qty){
        $query=mysqli_query($con,"insert into tblorders(ProductId,Quantity,InvoiceNumber,LorryName,CustomerName,CustomerAddress,PaymentMode) values('$pdid','$qty','$invoiceno','$lorryname','$customername','$customeraddress','$pmode')") ; // I AMENDED THIS LINE TO ADD IN LORRYNAME, CUSTOMERNAME, CUSTOMERADDRESS
    }
    echo '<script>alert("Invoice generated successfully. Invoice number is "+"'.$invoiceno.'")</script>';  
    unset($_SESSION["cart_item"]);
    $_SESSION['invoice']=$invoiceno;
    echo "<script>window.location.href='invoice.php'</script>";

}



// I ADDED THIS CODE BLOCK IN
<div class="col-md-4 mb-10">
                                                    <label for="validationLorry03">Lorry</label>
                                                    <select  name="lorryname"  class="form-control" required>
                                                        <option value="">Select Lorry</option>
                                                        <?php
                                                $sql="SELECT * from  tbllorries";
                                                $query = $dbh -> prepare($sql);
                                                $query->execute();
                                                $results=$query->fetchAll(PDO::FETCH_OBJ);
                                                if($query->rowCount() > 0)
                                                {
                                                    foreach($results as $row)
                                                    {
                                                        ?> 
                                                        <option value="<?php  echo $row->lorryname;?>"><?php echo "Lorry Name: " . $row->lorryname . " | Area: " . $row->lorryarea . " | Day: " . $row->lorryday;?></option>
                                                        <?php 
                                                    }
                                                }
                                                        ?>
                                                    </select>
                                                </div>



// I ADDED THIS CODE BLOCK IN

<div class="col-md-4 mb-10">
                                                    <label for="validationLorry03">Lorry</label>
                                                    <select  name="lorryname"  class="form-control" required>
                                                        <option value="">Select Lorry</option>
                                                        <?php
                                                $sql="SELECT * from  tbllorries";
                                                $query = $dbh -> prepare($sql);
                                                $query->execute();
                                                $results=$query->fetchAll(PDO::FETCH_OBJ);
                                                if($query->rowCount() > 0)
                                                {
                                                    foreach($results as $row)
                                                    {
                                                        ?> 
                                                        <option value="<?php  echo $row->lorryname;?>"><?php echo "Lorry Name: " . $row->lorryname . " | Area: " . $row->lorryarea . " | Day: " . $row->lorryday;?></option>
                                                        <?php 
                                                    }
                                                }
                                                        ?>
                                                    </select>
                                                </div>

                                                <div class="col-md-4 mb-10">
                                                    <label for="validationCustom03">Customer Name</label>

                                                    <script>
                                                        function myFunction(){
                                                            var index = document.getElementById("customername").selectedIndex;
                                                            //alert(index);
                                                            var add = document.getElementById("customername").options[index].getAttribute("data-add");

                                                            document.getElementsByName("customeraddress")[0].value = add;
                                                        }
                                                    </script>

                                                    <select  name="customername" id="customername" class="form-control" required onchange="myFunction();">
                                                        <option value="">Select Customer</option>
                                                        <?php
                                                $sql="SELECT * from  tblcustomers";
                                                $query = $dbh -> prepare($sql);
                                                $query->execute();
                                                $results=$query->fetchAll(PDO::FETCH_OBJ);
                                                if($query->rowCount() > 0)
                                                {
                                                    foreach($results as $row)
                                                    {
                                                        ?> 
                                                        <option value="<?php echo $row->customername;?>" data-add="<?php echo $row->customeraddress;?>"><?php echo $row->customername;?></option>
                                                        <?php 
                                                    }
                                                }
                                                        ?>
                                                    </select>
                                                </div>
                                                <div class="col-md-4 mb-10">
                                                    <label for="validationCustom03">Customer Address</label>
                                                    <textarea rows=5 cols=5 name="customeraddress" class="form-control" placeholder="Enter Customer Address" required></textarea>
                                                </div>

The code $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"]; means that, whatever the value within $_POST['quantity'], you want to add that value to $_SESSION["cart_item"][$k]["quantity"].

The error of unsupported operand types means that you're trying to add values that can't be added. For example, The value of $_POST["quantity"] might be the string "abc" and you can't add abc to a number, as that would make no sense.

As you point out, it's a ton of code for me to spend my time reading and sifting through, especially because a lot of it isn't commented, so it's hard to understand what each line or section is doing, since I can't follow your logic.

However, what I can be sure of is you're getting the error because one, or both, of the variables you're working with are not numbers. Trace your code and make sure that there are no edge cases where $_SESSION["cart_item"][$k]["quantity"] is not a number, does not exist, is null, etc. Also, type cast $_POST["quantity"] into floatval($_POST['quantity']) to force it to be a floating point number, since it's being retrieved from $_POST which, theoretically, anyone can pass in literally anything there since it's unsanitized user input. The line should now look like:

$_SESSION["cart_item"][$k]["quantity"] += floatval($_POST["quantity"]);

We can then guarantee that at least the number being passed in is a float. You now need to ensure that the session variable is also guaranteed in all cases to be a number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.