Forum Moderators: coopster
So, I add one or more products to my cart page, it shows their characteristics and their prices without a problem. But when there are more than one products in the cart, the total price gets wrong. It shows the correct price of the first product added plus 1 or 2 or 3 grr it's really strange.
So here is the code of the cart.php where I think the mistake is:
<?php
$total = 0;
while ($row = mysql_fetch_array ($results)) {
echo "<tr>";
extract ($row);
$prod = "SELECT * FROM products " .
"WHERE products_prodnum = '$carttemp_prodnum'";
$prod2 = mysql_query ($prod);
$prod3 = mysql_fetch_array($prod2);
extract($prod3);
echo "<td><form method=\"POST\" action=\"modcart.php?action=change\">
<input type=\"hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\">
<input type=\"text\" name=\"modified_quan\" size=\"2\" value =\"$carttemp_quan\">";
echo "</td><td>";
echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
echo "THUMBNAIL<br>IMAGE</a></td><td>";
echo "<a href=\"getprod.php?prodid=" . $products_prodnum . "\">";
echo $products_name;
echo "</a></td><td >";
echo $products_price;
echo "</td><td>";
$extprice = number_format ($products_price * $carttemp_quan, 2);
echo $extprice;
echo "</td><td>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Change quantity \"></form></td><td>";
echo "<form method=\"POST\" action=\"modcart.php?action=delete\">
<input type=\"hidden\" name=\"modified_hidden\" value=\"$carttemp_hidden\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Delete a product\"></form></td></tr>";
$total = $extprice + $total;
}
?>
<tr>
<td colspan="4">Price before shipping:</td>
<td><?php echo number_format ($total,2); ?></td>
<td></td>
</tr>
<td align="left" colspan="5">
<?php
echo "<form method=\"POST\" action=\"modcart.php?action=empty\"><input type=\"hidden\" name=\"carttemp_hidden\" value=\"";
if (isset ($carttemp_hidden) ) {
echo $carttemp_hidden;
}
echo "\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Empty cart\"></form>";
?>
I'm not sure if I have included all the needed information since I don't know what exactly the mistake is. But I'll appreciate any kind of help.
while ($row = mysql_fetch_array ($results)) {
............
$prod = "SELECT * FROM products " .
"WHERE products_prodnum = '$carttemp_prodnum'";
$prod2 = mysql_query ($prod);
$prod3 = mysql_fetch_array($prod2);
extract($prod3);
.............
$total = $extprice + $total;
}
First, your script begins with
while ($row = mysql_fetch_array ($results)) {
So what is the select and execute that produces $results?
The rest of it seems to make sense, $total should keep changing as it loops through the rows, but the problem may exist in the "missing select" above.
It seems I haven't pasted some of the code before $total = 0 :
<?php
$sessid = session_id ();
$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query ($query) or die (mysql_query () );
$rows = mysql_num_rows ($results);
echo $rows;
?>
I changed the line $total = $extprice + $total with the line $total = $products_price * $carttemp_quan + $total; and it worked! I still don't know why it made a problem before but at least it works now.