问题
Does anyone know what problem that I encountered? The code work quite fine this few weeks, but it's suddenly appear such error messages. These are the entire code that I using before.
if (isset($_POST["add"])){
if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
}
$_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
$_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = $_SESSION["cart"][$_GET["id"]]['item_quantity'] + $_POST["quantity"];
header('Location: counter.php');
}
if (isset($_POST["minus"])){
if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
}
$_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
$_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = ( ( ( $_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] ) > 1 ) ?
$_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] : 1 );
header('Location: counter.php');
}
<?php
if(isset($_GET['category_id'])){
$cat_id = $_GET['category_id'];
$query = "SELECT product_id, product_name, product_price, image FROM product WHERE type = '$cat_id' ORDER BY product_id ASC";
$result = mysqli_query($connect,$query);
if(mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
?>
<div class="col-md-3">
<form method="post" action="counter.php?action=add&id=<?php echo $row["product_id"]; ?>">
<div class="product">
<img src="img/<?php echo $row["image"]; ?>" style="width:100px; height:100px">
<h5 class="text-info"><?php echo $row["product_name"]; ?></h5>
<h5 class="text-danger"><?php echo "RM " . $row["product_price"]; ?></h5>
<input type="text" name="quantity" class="form-control" value="1">
<input type="hidden" name="hidden_name" value="<?php echo $row["product_name"]; ?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["product_price"]; ?>">
<input type="submit" name="add" style="margin-top: 5px;" class="btn btn-success" value="+">
<input type="submit" name="minus" style="margin-top: 5px;" class="btn btn-success" value="-">
</div>
</form>
</div>
<?php
}
}
}
?>
The error message is start on
It shows Fatal error: Uncaught Error: Cannot use string offset as an array in C:\xampp\htdocs\surfmart1\counter.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\surfmart1\counter.php on line 17
if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
}
$_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
$_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = $_SESSION["cart"][$_GET["id"]]['item_quantity'] + $_POST["quantity"];
来源:https://stackoverflow.com/questions/54746988/fatal-error-uncaught-error-cannot-use-string-offset-as-an-array-in-stack-tra