Home > other >  Fatal error: Uncaught TypeError: Unsupported operand types: string * string
Fatal error: Uncaught TypeError: Unsupported operand types: string * string

Time:06-20

Here is the full error:

Fatal error: Uncaught TypeError: Unsupported operand types: string * string in C:\xampp\htdocs\HomemadeNepal\purchase.php:22 Stack trace: #0 {main} thrown in C:\xampp\htdocs\HomemadeNepal\purchase.php on line 22

Here is the code:

<?php
include('conn.php');
if (isset($_POST['productid'])) {

    $customer = $_POST['customer'];
    $number = $_POST['number'];
    $sql = "insert into purchase (customer,number, date_purchase) values ('$customer','$number', NOW())";
    $conn->query($sql);
    $pid = $conn->insert_id;

    $total = 0;

    foreach ($_POST['productid'] as $product) :
        $proinfo = explode("||", $product);
        $productid = $proinfo[0];
        $iterate = $proinfo[1];
        $sql = "select * from product where productid='$productid'";
        $query = $conn->query($sql);
        $row = $query->fetch_array();

        if (isset($_POST['quantity' . $iterate])) {
            $subt = $row['price'] * $_POST['quantity' . $iterate]; <!--THIS IS THE LINE 22 WITH THE ERROR)
            $total  = $subt;

            $sql = "insert into purchase_detail (purchaseid, productid, quantity) values ('$pid', '$productid', '" . $_POST['quantity' . $iterate] . "')";
            $conn->query($sql);
        }
    endforeach;

    $sql = "update purchase set total='$total' where purchaseid='$pid'";
    $conn->query($sql);
    header('location:Thankyou.php');
} else {
?>
    <script>
        window.alert('Please select a product');
        window.location.href = 'order.php';
    </script>
<?php
}
?>

CodePudding user response:

$row['price'] and $_POST['quantity' . $iterate] return strings which can not be multiplied. So you have to convert both to int values:

$subt = (int)$row['price']* (int)$_POST['quantity' . $iterate]

CodePudding user response:

You can use intval() function

$subt = (intval)$row['price'] * (intval)$_POST['quantity' . $iterate]
  •  Tags:  
  • php
  • Related