I have created a simple add to cart app in PHP. but it isn't functioning properly. at the cart page it only showing product 1 details.and the output is persisted with product1

here is my code…

    <?php
session_start();
$conn=mysqli_connect('localhost','root','...','ecom');
if(isset($_POST['add'])){
    if(isset($_SESSION['SC'])){
        $item_array_id=array_column($_SESSION['SC'],'item_id');
        if(!in_array($_GET['id'],$item_array_id)){
            $count=count($_SESSION['SC']);
            $item_array=array(
                'item_id'=> $_GET['id'],
                'item_name'=>$_POST['name'],
                'item_price'=>$_POST['pricehi'],
                'item_quantity'=>$_POST['qty']
            );
            $_SESSION['SC'][$count]=$item_array;
        }else{
            echo'<script>alert(item already added)</script>';
            echo'<script>window.location=index.php</script>';
        }

    }else{
        $item_array=array(
            'item_id'=> $_GET['id'],
            'item_name'=>$_POST['name'],
            'item_price'=>$_POST['pricehi'],
            'item_quantity'=>$_POST['qty']
        );
        $_SESSION['SC'][0]=$item;
    }
}
if(isset($_GET['action'])){
    if($_GET['action']=='delete'){
        foreach($_SESSION['SC'] as $keys =>$val){
            if($val['item_id']==$_GET['id']){
                unset($_SESSION['SC']['$keys']);
                echo'<script>alert(item removed)</script>';
                echo'<script>window.location=index.php</script>';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    
   
    <title>Ecom</title>
</head>
<body>
<div class='container'>
    <?php
    $query='SELECT * FROM cart ';
    $result=mysqli_query($conn,$query);
    if(mysqli_num_rows($result)>0){
        while($row=mysqli_fetch_array($result)){
            ?>
            <div class="col-md-4">
            <form method='POST' action="cart.php?actin=add&id=<?php echo $row['id'];?>">
            <img src="<?php echo $row['image'];?>" >
            <h4 class='text-info'><?php echo $row['title'];?></h4>
            <h4 class='text-danger'><?php echo $row['price'];?></h4>
            <input type="text" name='qty' class='form-contro' value='1'>
            <input type="hidden" name='name'value=<?php echo $row['title'];?>>
            <input type="hidden" name='pricehi'value=<?php echo $row['price'];?>>
           <input type="submit" class='btn btn-primary' value='add to cart' name='add'>
            </form>


 </div>
         <?php
                    }
    }
           ?>
    </div>
      </body>
            </html>

here is cart.php code

<?php
session_start();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
    
</head>
<body>
    
    <div class='container'>
        <div class='table-responsive'>

        <h3>CART</h3>
            <table class='table table-bordered'>
                <tr>
                    <th width='40%'>Name</th>
                    <th width='10%'>Quantity</th>
                    <th width='20%'>price</th>
                    <th width='15%'>total</th>
                    <th width='5%'>Action</th>
                </tr>
                    <?php
                     if(!empty($_SESSION['SC'])){
                         $total=0;
                         foreach($_SESSION['SC'] as $key=>$val){
                             ?>
                             <tr>
                                <td><?php echo $val['item_name'];?></td>
                                <td><?php echo $val['item_quantity'];?></td>
<td><?php echo $val['item_price'];?></td>
                                <td><?php echo number_format ($val['item_quantity'] * $val['item_price'], 2);?></td>
                                <td><a href="cart.php=action=delete&id=<?php echo $val['item_id'];?>"><span class='text-danger'>Remove</span></a></td>
                             </tr>
                             <?php
                              $total= $total+($val['item_quantity'] * $val['item_price']);
                         }
                         ?>
                         <tr>
                         <td colspan=3 align='right'>total</td>
                         <td align='right'><?php echo number_format($total,2);?></td>

                         </tr>
                            
                         <?php
                     }
                    ?>
            
            </table>
        </div>
    </div>
</body>
</html>

```and the output is persisted with product1

Name    Quantity    price   total   Action
a.apple     1   100     100.00  Remove
total   100.00

Try var_dump($_SESSION[‘SC’]); to check what you really have in the session variable.

The problem might be with how you add items to the cart. After the statement which adds the item to the cart in the first code presented, do var_dump($_SESSION[‘SC’]); and see the if it’s always only one element in the array.