Jump to content

error in shopping cart code


Jaswinder
Go to solution Solved by Jaswinder,

Recommended Posts

Friends  i was watching the shopping cart tutorial ,, but stuck in midway.. but i was unable to find my error... here is the code

 

cart.php

<?php require('connection.php'); ?>

<h1>View Cart</h1>
<a href="index.php?page=products" title="Go to products Page">Products page</a>
<?php

 

    $i="select * from products where id_products IN ("; 

   foreach($_SESSION['cart'] as $id => $value)

   { $i.= $id. ","; }

  $i= substr($i,0,-1).") ORDER BY name ASC";

    $q=mysql_query($i) or die(mysql_error());
    if(empty($q))
    {
        echo " you need to an item first";
    }
    ?>
    <form method="post" action="index.php?page=cart">
    <fieldset>
    <table>
    <tr>
    <th>Name</th>
    <th>Description</th>
    <th>Unit price</th>
    </tr>
    <?php
    $i="select * from products where id_products IN (";

    foreach($_SESSION['cart'] as $id => $value)

   { $i.= $id.","; }

   $i= substr($i,0,-1).") ORDER BY name ASC";

        $q=mysql_query($i) or die(mysql_error());
    $total_price=0;
    if(!empty($q))
    {
        while($f=mysql_fetch_array($q))
        {
            $subtotal=$_SESSION['cart'][$f['id_products']]['quantity']*$f['price'];
            $total_price += $subtotal;
            ?>
            <tr>
            <td><?php echo $f['name']; ?></td>
            <td><input type="text" name="quantity-<?php echo $f['id_products'];?>" size="5" value="<?php echo $_SESSION['cart'][$f['id_products']]['quantity'];?>" /></td>
            <td><?php echo "$".$f['price']; ?></td>
            <td><?php echo "$".$_SESSION['cart'][$f['id_products']]['quantity']*$f['price']; ?></td>
            </tr>
            <?php
        }
    }
    ?>

 

error is

 

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 ') ORDER BY name ASC' at line 1

 

but the query is exactly same as tutorial.. but there it was working fine..but not with me ???

Edited by Jaswinder
Link to comment
Share on other sites

your need to echo out your $i variable so that you can see exactly what the query statement is (the die(...) logic should be outputing the the query statement in $i along with the mysql_error message). i'm betting there's nothing in the cart and the code shouldn't even being trying to run the database queries.

 

if this is the case, the code should be testing if $_SESSION['cart'] contains any items first.

 

note: most code you find posted on the Internet is crap. in this case, the code is already forming and running the query statement (to find if the cart is empty, which is incorrect logic in itself), then forming and running that exact same query statement again. what a waste.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.