php - SESSION Cart adding 2 + or -2 problem -
php - SESSION Cart adding 2 + or -2 problem -
so edited own shop im having issues it, illustration add together 2 instead of 1 or removes 2 instead of 1,
you can see how looks on www.neobotmx.org/test/tienda.php <<< not opwn public yet >> thats why on test folder
the shop code :
<?php $product_id = $_get[id]; //the product id url $action = $_get[action]; //the action url //if there product_id , product_id doesn't exist display error message if($product_id && !productexists($product_id)) { die("error. product doesn't exist"); } switch($action) { //decide case "add": $_session['cart'][$product_id]++; //add 1 quantity of product id $product_id break; case "remove": $_session['cart'][$product_id]--; //remove 1 quantity of product id $product_id if($_session['cart'][$product_id] == 0) unset($_session['cart'][$product_id]); //if quantity zero, remove (using 'unset' function) - otherwise show zero, -1, -2 etc when user keeps removing items. break; case "empty": unset($_session['cart']); //unset whole cart, i.e. empty cart. break; } ?> <?php if($_session['cart']) { //if cart isn't empty //show cart echo "<table border=\"1\" align=\"center\" padding=\"3\" width=\"70%\">"; echo "<tr>"; //show info in table cells echo "<td align=\"center\"><strong>producto</strong></td>"; //along 'remove' link next quantity - links page, action of remove, , id of current product echo "<td align=\"center\"><strong>cantidad</strong></td>"; echo "<td align=\"center\"><strong>costo</strong></td>"; echo "</tr>";//format cart using html table //iterate through cart, $product_id key , $quantity value foreach($_session['cart'] $product_id => $quantity) { //get name, description , cost database - depend on database implementation. //use sprintf create sure $product_id inserted query number - prevent sql injection $sql = sprintf("select name, description, cost products id = %d;", $product_id); $result = mysql_query($sql); //only display row if there product (though there should have checked) if(mysql_num_rows($result) > 0) { list($name, $description, $price) = mysql_fetch_row($result); $line_cost = $price * $quantity; //work out line cost $total = $total + $line_cost; //add total cost echo "<tr>"; //show info in table cells echo "<td align=\"center\"><strong>$name</strong></td>"; //along 'remove' link next quantity - links page, action of remove, , id of current product echo "<td align=\"center\"><strong>$quantity </strong><a href=\"$_server[php_self]?action=remove&id=$product_id\">borrar</a></td>"; echo "<td align=\"center\"><strong>$line_cost</strong></td>"; echo "</tr>"; } } //show total echo "<tr>"; echo "<td colspan=\"2\" align=\"right\"><strong>total</strong></td>"; echo "<td align=\"right\"><strong>$total</strong></td>"; echo "</tr>"; echo "</table>"; }else{ //otherwise tell user have no items in cart echo "no tiene articulos en compra."; } //function check if product exists function productexists($product_id) { //use sprintf create sure $product_id inserted query number - prevent sql injection $sql = sprintf("select * products id = %d;", $product_id); homecoming mysql_num_rows(mysql_query($sql)) > 0; } ?> </p> <p><strong><a href="tienda.php">seguir comprando</a></strong></p> <?php
and display of books / items / whatever want.
<?php define('max_rec_per_page', 1); $sql = "select id, name, description, cost products;"; $rs = mysql_query("select count(*) products") or die("imposible realizar operacion"); list($total) = mysql_fetch_row($rs); $total_pages = ceil($total / max_rec_per_page); $page = intval(@$_get["page"]); if (0 == $page){ $page = 1; } $start = max_rec_per_page * ($page - 1); $max = max_rec_per_page; $rs = mysql_query("select id, name, description, cost products order id asc limit $start, $max") or die("imposible realizar operacion"); ?> <table width="100%" height="404" border="0" cellpadding="12"> <?php while (list($id, $name, $description, $price) = mysql_fetch_row($rs)) { ?> <tr> <td height="46" align="left" valign="middle"><p><strong> producto : <?= htmlspecialchars($name) ?> </strong> </p></td> </tr> <tr> <td height="172" align="left" valign="middle"><p><strong>descripcion :</strong></p> <p> <strong> <?= htmlspecialchars($description) ?> </strong></p></td> </tr> <tr> <td height="67" align="left" valign="middle"><p><strong>precio : <?= htmlspecialchars($price) ?> </strong> </p></td> </tr> <tr> <td height="109" align="center" valign="middle"><strong><? echo "<a href=\"pedido.php?action=add&id=$id\">comprar</a>" ?> </strong></td> </tr> <?php } ?> </table> <table border="0" cellpadding="5" align="center"> <tr> <td><strong>pagina : </strong></td> <?php ($i = 1; $i <= $total_pages; $i++) { $txt = $i; if ($page != $i) $txt = "<a href=\"" . $_server["php_self"] . "?page=$i\">$txt</a>"; ?> <td align="center"><?= $txt ?></td> <?php } ?> </table>
i have no thought where's error on it...
ty help :)
obiusly have :
<?php session_start();?> include database etc
you have in style:
body { background-image: url(); }
which causing browser request page again, adds cart again.
instead of rendering cart page, 1 time code has modified cart should send redirect cart page.
php sql
Comments
Post a Comment