Jump to content
Killersites Forums

Mysql Bug In Shopping Cart Tutorial


Recommended Posts

Hi,

 

there seems to be a bug in the shopping cart tutorial:

 

Have a look at the demo-version on http://www.killervideostore.com/video-courses/live-demos/PHPCARTOOP/

 

If you put only 1 item in the cart and you manually change the quantity in the input field from 1 to 0,

you will get 2 error messages:

 

Warning: mysqli::query() [mysqli.query]: (42000/1064): 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' at line 1 in /home/killervi/public_html/video-courses/live-demos/PHPCARTOOP/app/models/m_products.php on line 165

Warning: mysqli::query() [mysqli.query]: (42000/1064): 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' at line 1 in /home/killervi/public_html/video-courses/live-demos/PHPCARTOOP/app/models/m_products.php on line 47

 

This seems to relate to the following MySQL-query in m_products.php within the functions get() and get_prices():

 

...
if ($result = $this->Database->query("SELECT id, name, description, price, image FROM $this->db_table WHERE id IN ($items) ORDER BY name"))
{
...
}

 

Does anyone know how to fix this?

Link to post
Share on other sites

I tried emailing you back, but I always got an error when trying to email your email address unfortunately.

 

As for your question, I imagine it's not technically something that is wrong with the syntax, but that $items is either null or empty and the query doesn't know how to handle that (since that isn't an expected value). The first step would be do figure out what $items contains within the get() method when the error occurs, and then work back from there.

 

I'd likely have to step through the entire logic of the page request to see how $items is set in order to resolve it. I'll see if I can take a look near the end of the week.

Link to post
Share on other sites

So I looked into this. Setting a product to "0" is fine... as long as it isn't the only item in the cart. Basically, the issue is that the $_SESSION['cart'] could wind up being empty... and the mysqli queries didn't know how to handle an empty array. 

 

Update m_cart.php:

public function update($id, $num) 
{
    if ($num == 0)
    {
        unset($_SESSION['cart'][$id]);
        if (empty($_SESSION['cart'])) { unset($_SESSION['cart']); } //add
    }
    else 
    {
        $_SESSION['cart'][$id] = $num; 
    }
}

The fix in this case was to check if the session variable is empty, and if so, unset the cart so there are 0 items in the cart. 

 

Hope that helps.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...