SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast owentech's Avatar
    Join Date
    Apr 2006
    Location
    Nairobi, Kenya
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bug in Shopping Cart Array

    Hi,
    I have the following shopping cart that works perfectly on my home PC.
    Code:
    <?php
    // start session
    session_start();
    ?>
    <!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>my title</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    <center>
    <table align="center" class="maintable">
    <tr>
    <td colspan="2">
        <!--Top Block Starts -->
        <?php include('inctop.php'); ?>
        <!--Top Block Ends -->
    </td>
    </tr>		
    
    <!--Left Block Starts -->	
    <tr>
    <td class="blk2_lp" width="30%" valign="top">                    
        <?php include('inccategories.php'); ?>
    </td>
    <!--Left Block Ends -->
            
    <!--Right Block Starts -->
    <td valign="top">
    <span class="wel">Your Shopping Cart</span><br><br>
        <?php
            include('[path to connection file].php');
            if ($_GET['itemtoadd'])
            {
                $itemtoadd = $_GET['itemtoadd'];
                $_SESSION['shoppingcart'][$itemtoadd] = 1;
            }
            if ($_GET['itemtoremove'])
            {
                $itemtoremove = $_GET['itemtoremove'];
                unset($_SESSION['shoppingcart'][$itemtoremove]);
            }
            if (isset($_POST['cmdupdatecart'])) 
            {
                foreach ($_POST['user_qty'] as $cartitem => $newqty) 
                {
                    if ( $newqty== 0 ) 
                    {
                        unset ($_SESSION['shoppingcart'][$cartitem]);
                    } 
                    elseif ( $newqty > 0 ) 
                    {
                        $_SESSION['shoppingcart'][$cartitem] = $newqty;
                    }
                    next($_POST['user_qty']);
                }
            }       
            $mycart = $_SESSION['shoppingcart'];
            echo "<form action ='shoppingcart.php' method ='post'>";
            echo "<table ALIGN=\"center\" WIDTH=\"90%\" class=\"noborders\">";
            if (is_array($mycart))
            {
                echo "<TR class=\"header\">";
                echo "<TD width = \"5%\">&nbsp;</td>";
                echo "<TD width = \"20%\">&nbsp;</td>";
                echo "<TD width = \"35%\"><B>Item</B></font>&nbsp;</td>";
                echo "<TD width = \"10%\">Unit Price</td>";
                echo "<TD width = \"10%\">Quantity</td>";
                echo "<TD width = \"10%\">Total Price</td>";
                echo "<TD width = \"10%\">Remove</td>";
                echo "</TR>";
                $dbconnection = mysql_connect($dbhost,$dbusername,$dbpassword);
                mysql_select_db($database,$dbconnection );
                $counter = 1;
                $totalprice = 0;
                foreach ($mycart as $itemqty) 
                {
                    $cartitemid = key($mycart);
                    $sql = "SELECT * FROM tblitems WHERE itemid = $cartitemid";
                    $result = mysql_query($sql,$dbconnection) or die(mysql_error());
                    $myrow = mysql_fetch_array($result);
                    $itemid = $myrow['itemid'];
                    $itemname = $myrow["itemname"];
                    $itemprice = $myrow["itemprice"];
                    $itemtotalprice = $itemprice * $itemqty;
                    $itempicturename = (empty($myrow["itempicture"])) ? 'default.jpg' : $myrow["itempicture"];
                    $itempicture = "<img src=\"demo/itempics/$itempicturename\" width=\"100\" height=\"100\">";
                    $mysubcategoryidf = $myrow["subcategoryidf"];
                    $subcategoryname = getsubcategoryname($mysubcategoryidf);
                    if($counter %  2) {
                        echo "<TR>";
                    } else {
                        echo "<TR bgcolor=\"#CCCCCC\">\n";
                    }//colour has now been set :-)
                    echo "<TD>$counter</td>";
                    echo "<TD>$itempicture</td>";
                    echo "<TD>$itemname</td>";
                    echo "<TD align='right'>".number_format($itemprice,2)."</td>";
                    echo "<TD align='right'><input size='4' type='text' name=\"user_qty[".$itemid."]\" value='$itemqty'></td>";
                    //echo "<TD>$itemqty</td>";
                    echo "<TD align='right'>".number_format($itemtotalprice,2)."</td>";
                    echo "<TD><a href=\"shoppingcart.php?itemtoremove=$itemid\">Remove</a></td>";
                    echo "</tr>";
                    $counter = $counter + 1;
                    $totalprice = $totalprice + $itemtotalprice;
                    next($mycart);
                }
                echo "<tr class=\"header\">";
                echo "<td colspan='5' align='right'>Your Total Price is:</td>";
                echo "<td align='right'><b>".number_format($totalprice,2)."</b></td>";
                echo "<td>&nbsp;</td>";
                echo "</tr>";
                
                echo "<tr>";
                echo "<td colspan='5' align='right'>Update Cart</td>";
                echo "<td align='right'><input name=\"cmdupdatecart\" value=\"Update\" type=\"submit\"></td>";
                echo "<td>&nbsp;</td>";
                echo "</tr>";
    
                echo "<tr>";
                echo "<td colspan='5' align='right'>--</td>";
                echo "<td align='right'><a href='shoppingcheckout.php'>Checkout</a></td>";
                echo "<td>&nbsp;</td>";
                echo "</tr>";            
            }
            else
            {
                echo "<tr><td>Your Shopping cart is currently empty</td><tr>";
            }
            echo "</table>";
            echo "</form>";
            function getsubcategoryname($subcategoryidf){
                $subcategoryresult = mysql_query("SELECT * FROM tblsubcategories WHERE subcategoryid = $subcategoryidf");
                $mysubcategory = mysql_fetch_array($subcategoryresult);
                $subcategoryname = $mysubcategory['subcategoryname'];
                return $subcategoryname;
            }
    
            ?>
    </td>
    <!--Right Block Ends -->
    
    <!--Content Block Ends -->
    </tr>
    
    <tr>
    <td colspan="2">	
    	<div id="sub_block2">
            <?php include('incbott.php'); ?>
    	</div>
    </td>
    </tr>
    </table>
    </center>
    </body>
    </html>
    What happens when I try to run the script online is:
    1) I successfully add the first itrm to the Cart and the shopping cart displays
    2) When I add additional items, I get an error saying:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    The error appears above the Shopping cart
    3) All the other items in the cart display except the first one
    4) The Update and Checkout links do NOT display

    What am I missing?
    Life is too short to think small - John Mason
    What is any life if not the pursuit of a dream? - Vanilla Sky
    PHP Membership Script

  2. #2
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $subcategoryresult mysql_query("SELECT * FROM tblsubcategories WHERE subcategoryid = $subcategoryidf"); 
    What happens when $subcategoryidf is blank in above query?

  3. #3
    SitePoint Enthusiast owentech's Avatar
    Join Date
    Apr 2006
    Location
    Nairobi, Kenya
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have not tried that, but I do not think that has arisen.
    And even if it did, why would it do that only for the first item, regardless of what item that is?
    But I will look into it.
    Maybe the function is running without a value being passed.
    Life is too short to think small - John Mason
    What is any life if not the pursuit of a dream? - Vanilla Sky
    PHP Membership Script

  4. #4
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by owentech View Post
    I have not tried that, but I do not think that has arisen.
    And even if it did, why would it do that only for the first item, regardless of what item that is?
    But I will look into it.
    Maybe the function is running without a value being passed.
    if $subcategoryidf is blank then there will be mysql error as the query will be invalid.
    In such cases using values in quotes(suited for mysql only) may save you from such errors.

    Thanks

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    too hard to read that code (use the php tags, there is a button), but, echo all queries, copy paste and run them on the server (phpmyadmin or whatever).

    If one of the queries does have an error, find out what it is, and then find out how it is generated in the code.

  6. #6
    SitePoint Enthusiast owentech's Avatar
    Join Date
    Apr 2006
    Location
    Nairobi, Kenya
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    I replaced
    PHP Code:
                foreach ($mycart as $itemqty
                {
                    
    $cartitemid key($mycart); 
    with
    PHP Code:
    foreach ($mycart as $cartitemid => $itemqty
                { 
    and it worked!
    :-)

    Thanks
    Life is too short to think small - John Mason
    What is any life if not the pursuit of a dream? - Vanilla Sky
    PHP Membership Script


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •