您好, 我有一个与此类似的问题,但似乎我又被卡住了。我以为我可以让它工作,直到我尝试将多个产品添加到购物车并将其发送到数据库。
插入用户数据(姓名、地址等)和1件商品(产品名称、价格等)的购物车数据时没有任何问题。但是,如果我说,当我尝试将数据发送到数据库时,购物车中有 2件商品,我也会得到 2 行,所以这没有任何问题,但是这两行是相同的商品,所以看起来计算相同的id两次,但session中实际上有 2 个不同的 id。
任何线索我哪里出错了?
我正在通过表格发送数据!
这是代码块:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO ordermodul (orgnummer, namn, levadress, faktadress, telefon, email, datum, summa, nmr) VALUES (%s, %s, %s, %s, %s, %s, now(), %s, %s)",
GetSQLValueString($_POST['orgnum'], "text"),
GetSQLValueString($_POST['namn'], "text"),
GetSQLValueString($_POST['levadress'], "text"),
GetSQLValueString($_POST['faktadress'], "text"),
GetSQLValueString($_POST['telefon'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['summa'], "text"),
GetSQLValueString($_POST['nmr'], "text"));
mysql_select_db($database_vesundberg_eu, $vesundberg_eu);
$Result1 = mysql_query($insertSQL, $vesundberg_eu) or die(mysql_error());
$order_id = mysql_insert_id();
if ($order_id) {
$quantity = $each_item['quantity'];
implode ( ',', array_values($each_item));
$line_cost = $price;
foreach ($_SESSION['cart_array'] as $each_item) {
$item_id = $each_item['item_id'];
$sql = sprintf("SELECT id, productname, price FROM products WHERE id = %d;", '$item_id');
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
list($id, $price) = mysql_fetch_row($result);
$line_cost = number_format($price * $quantity, 2);
}
$query = sprintf("INSERT INTO cartorders (id, nmr, name, price, quantity) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString("$artnummer", "int"),
GetSQLValueString("$nmr", "int"),
GetSQLValueString("$product_name", "text"),
GetSQLValueString("$line_cost", "int"),
GetSQLValueString("$quantity", "int"));
mysql_select_db($database_vesundberg_eu, $vesundberg_eu);
$Result1 = mysql_query($query, $vesundberg_eu) or die(mysql_error());
}}
$insertGoTo = "orders.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
这是表格:
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table width="60%" border="0" align="center">
<tr align="center">
<td align="right">Org. nummer:</td>
<td align="left"><input name="orgnum" id="orgnum" type="text" /> *</td>
</tr>
<tr align="center">
<td align="right">Företagsnamn:</td>
<td align="left"><input name="namn" type="text" id="namn" /> *</td>
</tr>
<tr align="center">
<td align="right">Lev. adress:</td>
<td align="left"><input name="levadress" type="text" id="levadress" /> *</td>
</tr>
<tr align="center">
<td align="right">Fakt. adress:</td>
<td align="left"><input name="faktadress" type="text" id="faktadress" /> *</td>
</tr>
<tr align="center">
<td align="right">Telefon:</td>
<td align="left"><input name="telefon" type="text" id="telefon" /> *</td>
</tr>
<tr align="center">
<td align="right">E-mail:</td>
<td align="left"><input name="email" type="text" id="email" /> *</td>
</tr>
<tr align="center">
<td align="right"></td>
<td align="left"><input name="summa" type="hidden" id="summa" value="<?php echo $cartTOTAL; ?>" /></td>
</tr>
<tr align="center">
<td align="right"></td>
<td align="left"><input name="prodnum" type="hidden" id="prodnum" value="<?php echo $artnummer; ?>" /></td>
</tr>
<tr align="center">
<td align="right"></td>
<td align="left"><input name="prodname" type="hidden" id="prodname" value="<?php echo $product_name; ?>" /></td>
</tr>
<tr align="center">
<td align="right"></td>
<td align="left"><input name="price" type="hidden" id="price" value="<?php echo $price; ?>" /></td>
</tr>
<tr align="center">
<td align="right"></td>
<td align="left"><input name="quantity" type="hidden" id="quantity" value="<?php echo $each_item['quantity']; ?>" /></td>
</tr>
<tr align="center">
<td align="right"></td>
<td align="left"><input name="nmr" type="hidden" id="nmr" value="<?php echo $nmr; ?>" /></td>
</tr>
<tr align="center">
<td align="right"> </td>
<td align="left"><input name="submit" type="submit" value="Skicka Order" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>