Reputation: 160
Hi there, I had a question up similar to this one, but seems like I'm stuck again. I thought i got it to work until i tried to add more than one product to the cart and sending it to the database.
The user data (name, address, etc..) and the cart data for 1 item (product name, price, etc..) gets inserted without any problem. However, if i have lets say, 2 items in the cart when i try to send the data to the database, i get 2 rows as well so there it isn't any problem, but both rows are the same item, so it seem to count the same id twice, but there actually are 2 different id's in the session.
Any clues where I've gone wrong?
I'm sending the data through a form!
Here's the code block:
$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));
}
Here's the form:
<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>
Upvotes: 0
Views: 1796
Reputation: 10433
I suspect that the id you are pulling from products is the same in each pass because
sprintf("SELECT id, productname, price FROM products WHERE id = %d;", '$item_id');
should evaluate to "SELECT id, productname, price FROM products WHERE id = 0 "
Because '$item_id' evaluates to the string "$item_id" which is not a number, rather than a string of the number. This answer has a nice break down of variable evalutation in strings
Change it to :
$sql = sprintf("SELECT id, productname, price FROM products WHERE id = %d;", $item_id);
Another issue is
list($id, $price) = mysql_fetch_row($result);
The returned row from $results
is made up of ("id","product_name", "price")
, so I'm guessing that $price
actually has the product name
in it, and price
is going no where.
Upvotes: 1