我设置的购物车存在一些问题,它不会显示购物车中的每个商品,而是显示正确数量的商品,但每次都会重复添加到购物车的第一个商品。
我知道出了什么问题,但只是不确定如何修复它......基本上目前我只对记录集执行一个查询,这就是正在显示的内容。在我的 PHP 基础知识中,我会说我需要对购物车中的每个项目进行查询(最多只有 6 个,所以永远不会很大)。
购物车中的数字与数据库中的 ship_id 数字相同......因此我在想我可以放一些类似的东西:
$sql = "SELECT $ship_id, $ship_name, image
FROM $ship_infomation
WHERE $ship_id = $cartId;";
...在 foreach 循环内,但这会破坏事情
有人能指出指导我解决小问题的最佳方向吗?
非常感谢
购物车展示:
<?php
$cart = $_SESSION['cart'];
if (!$cart) {echo "<div class='dialogue_box_inside_info_request'>
<div class='dialogue_box_image'>
<img src='images/basket_empty_dialogue.png' width='618' height='264' />
</div>
</div>";}
else
{
$array = explode(',', $_SESSION['cart']);
echo "<table width='835' border = '0'>";
foreach($array as $cartId) {
$ship_name = $row_ships['ship_name'];
$ship_image = $row_ships['image'];
echo "<tr>";
echo "<td width='110' height='58' class='table_text'><img src='images/ships/$ship_image'width='83' height='53' /> </td>";
echo "<td width='620' height='58' class='table_text'>$ship_name</td>";
echo "<td width='35' height='58' class='table_text'><a href='cart.php?action=delete&ship_id=$cartId'><img src='images/trash.png' width='31' height='42' /></a></td>";
echo "<td height='58'>$cartId</td>";
echo "</tr>";
}
echo "</table>";
}
?>
记录集放置在代码顶部
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_ships, $ships);
$query_ships = "SELECT ship_id, ship_name, image FROM ship_infomation";
$ships = mysql_query($query_ships, $ships) or die(mysql_error());
$row_ships = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
?>