我正在创建一个迷你购物车,我想知道是否有办法从会话中获取数据并将其写入数据库。用户选择的零件应该被添加到他们的购物车中。完成后,我将如何获取存储到会话中的数据,并将其写入数据库,以便他们下次登录时他们的购物车仍然存在?
<?php
if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT partID, quantity, catergory FROM computerparts WHERE partID=' . mysql_real_escape_string((int) $_GET['add']));
while ($quantity_row = mysql_fetch_array($quantity)) {
if ($quantity_row['quantity'] != $_SESSION['index_' . $_GET['add']]) {
$_SESSION['index_' . $_GET['add']]+='1';
}
}
header('Location: ' . $page);
}
if (isset($_GET['delete'])) {
$_SESSION['index_' . (int) $_GET['delete']] = '0';
header('Location: ' . $page);
}
function cart() {
$total = "";
$sub = "";
foreach ($_SESSION as $name => $value) {
if ($value > 0) {
if (substr($name, 0, 6) == 'index_') {
$id = substr($name, 6, (strlen($name) - 6));
$get = mysql_query('SELECT partID, partName, price FROM computerparts WHERE partID=' . (int) $id);
while ($get_row = mysql_fetch_array($get)) {
$sub = $get_row['price'] * $value;
echo $get_row['partName'] . 'x' . $value . '@ £' . $get_row['price'] . '=' . $sub . '<a href="index.php?delete=' . $id . '">Delete</a> <br />';
}
}
$total += $sub;
}
}
if ($total == 0) {
echo 'Your cart is empty';
} else {
echo 'Total: £' . $total;
}
}
?>
为了让事情变得更容易,我已经从这个网站上删除了功能。将不会购买任何物品,所有发生的事情是用户选择他想要的物品并保存该信息,以便在他希望您可以将其引用到规范时随时访问。(规格表)他选择的项目将被写入该表。使用当前登录的用户 ID。