Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 29th, 2008, 11:38 AM
Authorized User
 
Join Date: Aug 2005
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default My own shoping cart

Hi could someone help me with this shopping cart.
I have taken this from a tutorial on web. Everything works fine except the remove item function
Can someone help with remove_item function


<?php
include("db.php");
    switch($_GET["action"])
    {
        case "add_item":
        {
            AddItem($_GET["ID"], $_GET["VarID"], $_GET["MetVar"], $_GET["Pintyp"], $_GET["Pinvar"], $_GET["qty"]);
            ShowCart();
            break;
        }
        case "update_item":
        {
            UpdateItem($_GET["ID"], $_GET["VarID"], $_GET["MetVar"], $_GET["Pintyp"], $_GET["Pinvar"], $_GET["qty"]);
            ShowCart();
            break;
        }
        case "remove_item":
        {
            RemoveItem($_GET["ID"], $_GET["VarID"], $_GET["MetVar"], $_GET["Pintyp"], $_GET["Pinvar"]);
            ShowCart();
            break;
        }
        default:
        {
            ShowCart();
        }
    }



    function AddItem ($PID, $VID, $METID, $PTID, $PVID, $qty)
    {
        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and PID = $PID and VID = $VID and METID = $METID and PTID = $PTID and PVID = $PVID");
        $row = mysql_fetch_row($result);
        $numRows = $row[0];

        if($numRows == 0)
        {
            // This item doesn't exist in the users cart,
            // we will add it with an insert query

            @mysql_query("insert into cart(cookieId, PID, VID, METID, PTID, PVID, qty) values('" . GetCartId() . "', $PID, $VID, $METID, $PTID, $PVID, $qty)");
        }
        else
        {
            // This item already exists in the users cart,
            // we will update it instead

            UpdateItem($PID, $VID, $METID, $PTID, $PVID, $qty);
        }
    }

    function UpdateItem($PID, $VID, $METID, $PTID, $PVID, $qty)
    {

        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

        if($qty == 0)
        {
            // Remove the item from the users cart
            RemoveItem($PID, $VID, $METID, $PTID, $PVID);
        }
        else
        {
            mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and PID = $PID and VID = $VID and METID = $METID and PTID = $PTID and PVID = $PVID");
        }
    }

    function RemoveItem($PID, $VID, $METID, $PTID, $PVID)
    {
        // Uses an SQL delete statement to remove an item from
        // the users cart

        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and PID = $PID and VID = $VID and METID = $METID and PTID = $PTID and PVID = $PVID");
    }

    function ShowCart()
    {
        // Gets each item from the cart table and display them in
        // a tabulated format, as well as a final total for the cart

        global $dbServer, $dbUser, $dbPass, $dbName;

        // Get a connection to the database
        $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

        $totalCost = 0;
        $result = mysql_query("SELECT cart.*, product.Name, product.Price, product.Descirpt, varit.Varit as PV, metallivarit.varit as MV, pinnityp.tyyppi, pinninvari.vari as PNV FROM pinninvari INNER JOIN (pinnityp INNER JOIN (metallivarit INNER JOIN (varit INNER JOIN (cart INNER JOIN product ON cart.PID = product.PID) ON varit.ID = cart.VID) ON metallivarit.ID = cart.METID) ON pinnityp.ID = cart.PTID) ON pinninvari.id = cart.PVID
where cart.cookieId = '" . GetCartId() . "' order by product.Name asc");
        ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="../Styles/aaria.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="frmCart" name="frmCart" method="get" action="">
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
    <tr>
     <td width="8" class="style5">&nbsp;</td>
     <td width="46" class="style5"><strong>Nimi</strong></td>
     <td width="110" class="style5"><strong>Tuotetieto</strong></td>
     <td width="37" class="style5"><strong>V&auml;ri</strong></td>
     <td width="104" class="style5"><strong>Metallinv&auml;ri</strong></td>
     <td width="95" class="style5"><strong>Pinnityyppi</strong></td>
     <td width="85" class="style5"><strong>Pinninv&auml;ri</strong></td>
     <td width="40" class="style5"><strong>Kpl</strong></td>
     <td width="66" class="style5"><strong>Hinta</strong></td>
     <td width="49" class="style5"><strong>Poista</strong></td>
    </tr>
    <?php

            while($row = mysql_fetch_array($result))
            {
                // Increment the total cost of all items
                $totalCost += ($row["qty"] * $row["Price"]);
                ?>
    <tr>
     <td class="style5">&nbsp;</td>
     <td valign="top" class="style5"><?php echo $row["Name"]; ?>&nbsp;</td>
     <td width="110" valign="top" class="style5"><?php echo $row["Descirpt"]; ?></td>
     <td valign="top" class="style5"><?php echo $row["PV"]; ?>&nbsp;</td>
     <td valign="top" class="style5"><?php echo $row["MV"]; ?>&nbsp;</td>
     <td valign="top" class="style5"><?php echo $row["tyyppi"]; ?>&nbsp;</td>
     <td valign="top" class="style5"><?php echo $row["PNV"]; ?>&nbsp;</td>
     <td valign="top" class="style5"><?php echo $row["qty"]; ?>&nbsp;</td>
     <td valign="top" class="style5"><?php echo number_format($row["Price"], 2, ".", ",");; ?> #8364;</td>
     <td valign="top" class="style5"><a href="cart.php?action=remove_item&amp;id=<?php echo $row["PID"];?>&amp;VarID=<?php echo $row["VID"];?>&amp;MetVar=<?php echo $row["METID"];?>&amp;Pintyp=<?php echo $row["PTID"];?>&amp;Pinvar=<?php echo $row["PVID"];?>">Poista</a> </td>
    </tr>
    <?php
            }
            ?>
    <tr>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
    </tr>
    <tr>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5"><div align="right"><strong>Total:</strong></div></td>
     <td class="style5"><strong><?php echo number_format($totalCost, 2, ".", ","); ?> #8364;</strong></td>
     <td class="style5">&nbsp;</td>
    </tr>
    <tr>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
     <td class="style5">&nbsp;</td>
    </tr>
</table>
<?php
    }

?>

</form>
</body>
</html>

thx
 
Old June 1st, 2008, 08:04 AM
Authorized User
 
Join Date: Aug 2005
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I solved the problem, I take the database primary key and delete row by primary key which is always unique






Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding products to a shoping cart Guerra BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 4 March 16th, 2009 10:15 AM
Shopping Cart inkrajesh ASP.NET 1.0 and 1.1 Basics 2 February 28th, 2006 03:08 AM
problem whit shoping card mbcreator PHP How-To 0 February 17th, 2006 05:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.