Jump to content

Recommended Posts

Posted

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?

Posted

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.

Posted

Hi,

 

thanks for your response.

 

My e-mail should work normally. I have no problems to receive anything...

 

Please let me know when you have a solution.

 

Thanks.

Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...