получение неопределенных смещений при нажатии кнопки «Добавить к карточке», если в коде ранее были определены те же столбцы.
show_product.php code
<?php
// show_product.php
session_start();
include 'connect.php';
include ('functions.php');
$productID = $_GET['productID'];
// get all needed product data
$product = get_product_data($productID);
$product_id = $product[0][4];
$product_code= $product[0][0];
$name = $product[0][1];
$description = $product[0][2];
$price = $product[0][3];
$image_name = 'images/' . $product_code . '.jpg';
?>
<!DOCTYPE html>
<html>
<head>
<title>Book Shop</title>
<link rel="stylesheet" type="text/css" href= "main.css" />
</head>
<body>
<h1>Book Shop</h1>
<div id="left_column">
<h2><?php echo $name; ?></h2>
<img src="<?php echo $image_name; ?>"/>
</div>
<div id="right_column">
<p><b>List Price:</b>
<?php echo '$' . $price; ?></p>
<h2>Description</h2>
<p > <?php echo $description; ?> </p>
<form action="add_to_cart.php" method ="get" >
<input type="hidden" name="productID" value ="<?php echo $product_id ; ?>" />
<b> Quantity: </b>
<input type="text" name="quantity" value ="1" size="2" />
<input type="submit" value="Add to Cart" />
</form>
</div>
</body>
</html>
add_to_cart.php
<?php
session_start();
include 'connect.php';
include 'functions.php';
// Create an empty cart if it does not exist
if (!isset($_SESSION['cart'])) {
$_SESSION['cart']=array();
}
// Add an item to the cart
$productID = $_GET['productID'];
$quantity = $_GET['quantity'];
if ($quantity > 0) {
$_SESSION['cart'][$productID]= round($quantity,0);
//create an array of current items in the cart
$items = array();
?>
<!DOCTYPE html>
<html>
<head>
<title>BOOK SHOP</title>
<link rel="stylesheet" type="text/css" href= "main.css" />
</head>
<body>
<h1>Your Cart </h1>
<?php
$grand_total = 0;
echo '<table border = "1"> <tr> <th> Product Name </th> <th> Price </th> <th> Qty</th> <th> Total</th></tr> ';
foreach ($_SESSION['cart']as $productID => $quantity) {
// get book data
$product =get_product_data($productID);
$items[$productID]['productCode'] =$product[0][0];
$items[$productID]['productName'] =$product[0][2];
$items[$productID]['listPrice'] = $product[0][3];
$items[$productID]['quantity']=$quantity;
$image_name = 'images/' . $product[0][0] . '.jpg';
$product_total = $product[0][3] * $quantity;
$grand_total +=$product_total;
echo '<tr>';
echo '<td>' . $items[$productID]['productName'] . '</td>';
echo '<td>' . $items[$productID]['listPrice'] . '</td> ';
echo '<td>' . $items[$productID]['quantity'] . '</td>';
echo '<td>' . sprintf('$%.2f',$product_total) . '</td>';
echo '</tr>';
}
}
echo '<tr> <td> </td> <td> </td> <td>TOTAL</td> <td>' . sprintf('$%.2f',$grand_total) . '</td> ';
echo '</table>';
?>
</body>
</html>
информация, взятая из show product, добавляется в таблицу в add to cart и показывает цену, как если бы обычная корзина делала то, что вы видите на amazon, но я получаю ошибки смещения в add_to_cart.php