データをデータベースに挿入したいのですが、カートに 4 つの製品を追加したときに、4 つのうち 2 つしか挿入されないなど、正しく機能しません。小計は、製品の最初の金額を小計として取得し、次に製品全体の合計金額を表示するように表示されます。私の数量がデータベースに挿入されません。
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price per item</th>
<th>Total cost</th>
</tr>
<?php
$sql = "SELECT * FROM productsc WHERE id_product IN (";
foreach ($_SESSION['cartCity'] as $id => $value) {
$sql .= $id . ",";
}
$sql = substr($sql, 0, -1) . ") ORDER BY name ASC";
$query = mysql_query($sql);
$total_price = 0;
if (!empty($query)) {
while ($row = mysql_fetch_assoc($query)) {
$subtotal = $_SESSION['cartCity'][$row['id_product']]['quantity'] * $row['price'];
$total_price += $subtotal;
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><input type="text" name="quantity-<?php echo $row['id_product']; ?>" size="5" value="<?php echo $_SESSION['cartCity'][$row['id_product']]['quantity']; ?>" /></td>
<td><?php echo "$" . $row['price']; ?></td>
<td><?php echo"$" . $_SESSION['cartCity'][$row['id_product']]['quantity'] * $row['price']; ?></td>
</tr>
<?php
if ($row = mysql_fetch_assoc($query)) {
$sql2 = "INSERT INTO order_details (name,quantity,price,subtotal) VALUES ('" . $row['name'] . "','" . $quantity . "','" . $row['price'] . "','" . $total_price . "')";
$query2 = mysql_query($sql2) or die(mysql_error());
}
}
}
?>
<tr>
<td></td>
<td></td>
<td>Total price:</td>
<td><?php echo"$" . $total_price; ?></td>
</tr>
</table>