2021年3月17日星期三

How to solve "Notice: Trying to access array offset on value of type null..... in line 39" troubling submitting form

I'm having trouble with my PHP submitting form. It has fully filled the if-else condition and the order amount does not exceed the available stock. but it keeps displaying the error "Stock is not enough to Order" and the form could not be submitted. and the error of Notice: Trying to access array offset on the value of type null in[MY FILE PATH ]on line 39displayed after the page is refreshed. I checked my database and it has no problem with it. I'm not sure why does this happens. Please help me.

    <?php     include "session.php";    $now = date("Y-m-d h:i:s");    if(isset($_POST['checkout'])) {      $sql = "SELECT * FROM cart WHERE user_id = '$user_id' ";      $query = $conn->query($sql);        $csql = "SELECT * FROM ordered_items ORDER BY order_id DESC";      $cquery = $conn->query($csql);      $order_number = mysqli_num_rows($cquery);        if($order_number == 0) {        $order_id = 100001;      }      else {        $crow = $cquery->fetch_assoc();        $order_id = $crow['order_id'] + 1;      }        $delivery_date = $_POST['delivery_date'];      $item_deliver_method = $_POST['item_deliver_method'];      //$grand_total = $_POST['grand_total'];            while ($row = $query->fetch_assoc()) {        $seller_id = $row['seller_id'];        $product_id = $row['product_id'];        $quantity = $row['cart_qty'];        $quantity_unit = $row['cart_unit'];        $purchase_price = $row['cart_price'] * $quantity;              $order_status = 1;        $datetime = date('Y-m-d H:i:s');          //To calculate the stock        $psSql = "SELECT * FROM products WHERE id = '$product_id' ";        $psquery = $conn->query($psSql);        $psRow = $psquery->fetch_assoc();                $product_stocks = $psRow['product_stock'];          if($product_stocks >= $quantity) {            $osql = "INSERT INTO `ordered_items`(`order_id`, `user_id`, `seller_id`, `product_id`, `quantity`,`quantity_unit`, `purchase_price`, `delivery_date`, `order_datetime`, `order_status`,`item_deliver_method`)             VALUES ('$order_id', '$user_id', '$seller_id', '$product_id', '$quantity','$quantity_unit', '$purchase_price', '$delivery_date', '$datetime', '$order_status','$item_deliver_method')";            if($conn->query($osql)) {                $product_name = $row['cart_name'];                $nsql = "INSERT INTO `notifications`(`notification_title`, `notification_date`, `notification_text`, `user_id`, `seller_id`, `order_id`, `product_id`, `user_notify`, `seller_notify`, `admin_notify`)                 VALUES                 ('New Order', '$now', '<b>Order No: #$order_id</b><br>Order for $quantity $quantity_unit $product_name is Placed Successfully', '$user_id', '$seller_id', '$order_id', '$product_id', 0, 0, 0)";                $conn->query($nsql);                                $product_stock = $product_stocks - $quantity;                $product_status = ($product_stock > 0) ? 1 : 0;                                                $psql = "UPDATE `products` SET `product_stock`='$product_stock', `product_status`='$product_status' WHERE id = '$product_id' ";                $conn->query($psql);                                $dsql = "DELETE FROM cart WHERE user_id = '$user_id' ";                $conn->query($dsql);                              header ('location: checkout-summary.php?pid='.$order_id.' ');            }        }        else {          echo '<script>alert("Stock is not enough to Order");</script>';        }      }      }  ?>  

This is line 39 from the codes above

$product_stocks = $psRow['product_stock'];

https://stackoverflow.com/questions/66684166/how-to-solve-notice-trying-to-access-array-offset-on-value-of-type-null-i March 18, 2021 at 11:04AM

没有评论:

发表评论