PHP 警告 array_column() 和 array_search()

PHP warning array_column() and array_search()

当我第一次点击“添加到购物车”时,购物车菜单侧边栏不显示任何内容。

第二次单击“添加到购物车”时显示购物车菜单侧边栏。

URL: https://www.bellastudio.pk/new/products.php?p_slug=olive-green

当我开始添加到购物车时,我收到了这个错误:

PHP 警告:array_column() 要求参数 1 为数组,

中给出的为空

PHP 警告:array_search() 要求参数 2 为数组,

中给出的为空

我不知道如何让它工作,感谢您的帮助。我是新手。

PHP

try {
    if(isset($_POST['product_name']))
 {
    $name = $_POST['product_name'];
 }

 if(isset($_POST['product_price']))
 {
    $price = $_POST['product_price'];
 }

 if(isset($_POST['product_itemno']))
 {
     $itemno = $_POST['product_itemno'];
 }

 if(isset($_POST['product_quantity']))
 {
    $qty = $_POST['product_quantity'];
 }

 if(isset($_POST['product_color']))
 {
    $color = $_POST['product_color'];
 }

 if(isset($_POST['product_size']))
 {
    $size = $_POST['product_size'];
 }

 if(isset($_POST['sleeves']))
 {
    $sleeves = $_POST['sleeves'];
 }

 if(isset($_POST['neck']))
 {
    $neck = $_POST['neck'];
 }
 
 
 $stockitemno = $itemno.'-'.$size;

    $host = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $host->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $host->prepare( "SELECT * FROM productstock INNER JOIN product ON product.p_id = productstock.pid INNER JOIN productsize ON productsize.sid = productstock.size_id WHERE product.p_itemno = :itemno AND productsize.sname = :size" );
    $stmt->bindValue( ':itemno', $itemno );
    $stmt->bindValue( ':size', $size );
    $stmt->execute();

    while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {

        // Current stock quantity
        $stockqty = $row[ 'stockqty' ];

        // No stock, bail early
        if ( !$stockqty ) {
            echo '<p>No available stock for that item.</p>';
        }

        // User didn't provide a quantity value
        else if ( !$qty ) {
            echo '<p>Please enter a quantity</p>';
        }

        // Lets add to the cart
        else {

            // Find product in cart
            $cartindex = array_search( $stockitemno, array_column( $_SESSION[ 'cart' ], 'stockitemno' ) );

            // Product info
            $product = null;

            // If already exists in cart
            if ( $cartindex !== false ) {
                $product = $_SESSION[ 'cart' ][ $cartindex ];
            }

            // Does not exit in cart, create new product info
            else {
                $product = [
                    'name'          => $name,
                    'price'         => $price,
                    'item_no'       => $itemno,
                    'qty'           => 0, // Default to none added
                    'color'         => $color,
                    'size'          => $size,
                    'stockitemno'   => $stockitemno,
                    'sleeves'       => $sleeves,
                    'neck'          => $neck,
                    'stockqty'      => $stockqty
                ];
            }

            // Track how many items were able to be added to the cart
            $totaladded = 0;

            // If users full amount is available
            if ( ( $product[ 'qty' ] + $qty ) <= $stockqty ) {
                $totaladded = $qty;
            }

            // Else add all available stock
            else if ( $stockqty - $product[ 'qty' ] ) {
                $totaladded = ( $stockqty - $product[ 'qty' ] );
            }

            // If we were able to add new items to cart
            if ( $totaladded ) {

                // Update product new qty
                $product[ 'qty' ] += $totaladded;

                // Update cart item
                if ( $cartindex !== false ) {
                    $_SESSION[ 'cart' ][ $cartindex ] = $product;
                }

                // Add new item to cart
                else {
                    $_SESSION[ 'cart' ][] = $product;
                }

                // Example: 5 item (size) were successfully added to your shopping cart.
                printf( '<p><strong>%d</strong> %s (%s)<br>was successfully added to your shopping cart.</p>', $totaladded, $name, $size );
            } else {
                echo '<p>Not enough stock</p>';
            }

        }

    }
} catch ( PDOException $e ) {
    echo 'Error: ' . $e->getMessage();
}

$host = null; 
$cart_count = count( $_SESSION[ 'cart' ] );

AJAX

$(document).ready(function()
{
    load_cart_data();
function load_cart_data()
    {
        $.ajax({
            url:"asset/includes/fetch_cart1.php",
            method:"POST",
            success:function(data)
            {
                $('.cart_details').html(data);
                $('.badge1').text(data.total_item);
            }
        });
    }
$(document).on('click','#add_to_cart',function (e) {
    
        var cart_count = $('.navbar-tool-badge').text();
    cart_count = parseInt(cart_count);
    
    if($('input[type=radio][name=size]:checked').length == 0)
      {
         $('.msg').html('Please choose size.');
         return false;
      } else {
    var product_name = $('#hidden-name').val();
  var product_price = $('#hidden-price').val();
  var product_itemno = $('#itemno').val();
  var product_quantity = $('.quantity').val(); 
  var product_color = $('#color').val();
  var product_size = $("input[name='size']:checked").val();
  var sleeves = $('#sleeves').val();
  var neck = $('#neck').val();
e.preventDefault();
 
$.ajax
({
type: "POST",
url: "asset/includes/insertcart.php",
 data:{product_name:product_name, product_price:product_price, product_itemno:product_itemno , product_quantity:product_quantity, product_color:product_color, product_size:product_size, sleeves:sleeves, neck:neck},
cache: false,
success: function(response)
{
    load_cart_data();
    $("#getCode").html(response);
$("#myModal").modal('show');

// remove cart count
$('.navbar-tool-badge').html(cart_count + 1);

} 
});
}
 });
 
$(document).on('click', '.delete', function(){
  var item_no = $(this).attr("id");
    var cart_count = $('.navbar-tool-badge').text();
    cart_count = parseInt(cart_count);
  
   $.ajax({
    url:"asset/includes/delete_item.php",
    method:"POST",
    data:{item_no:item_no},
    success:function(data)
    {
        if(data){
            load_cart_data();
            $('#cart-popover').popover('hide');
        }

        // remove cart count
        $('.navbar-tool-badge').html(cart_count - 1);
        
    }
   })
});
 
});

在第一次尝试时,您的 $_SESSION[ 'cart' ] 可能是空的,因此您会遇到错误。尝试验证空购物车,然后像这样进行搜索

if(!empty($_SESSION[ 'cart' ])){
         $cartindex = array_search( $stockitemno, array_column( $_SESSION[ 'cart' ], 'stockitemno' ) );
    }