Problem inserting cookie data into...

User 2063679 Photo


Registered User
22 posts

Hi everybody,

I am working on a shopping cart script for a friend. This will insert the cart items into a database, and everything is working fine except that the customer ID, that is stored in a cookie, is not inserted into the database. The cookie HAS BEEN SET, because I can see it in my browser cookies view. All other variables is inserting itself fine.

My code is the following:

<?php

$id = (isset($_COOKIE['sitefinder_id']));

[Database connect]

mysql_query("INSERT INTO shoppingcart (user, product, description, quantity, price, price_total) VALUES ('" . $id . "', '" . $_POST['product'] . "', '" . $_POST['description'] . "', '" . $_POST['quantity'] . "','" . $_POST['price'] . "', '" . $_POST['quantity'] * $_POST['price'] . "')") or die(mysql_error());

?>

Thanks.
User 133269 Photo


Registered User
2,900 posts

try echoing the cookie and checking you're not inserting text into a number field or something - usually that'd tell ya a mismatch error though...

also where is the cookie set?
If you set a cookie (ie send it to the browser), it won't be sent back until the next request and so the data won't be in $_COOKIE till then.

if thats the problem try setting a $_SESSION as well or instead of the cookie

Have fun
~ Fe Pixie ~
User 133269 Photo


Registered User
2,900 posts

also try this without the isset
$id = $_COOKIE['sitefinder_id'];
or you might be setting id to true
Have fun
~ Fe Pixie ~
User 2063679 Photo


Registered User
22 posts

It is still not working, else thank you, Fe.
User 133269 Photo


Registered User
2,900 posts

can you get it to echo out ok?

did you try stuffing it in a session instead of a cookie to start with?

I always use sessions instead of cookies - relying on the customers browser to store data seems a silly idea to me :rolleyes:
Have fun
~ Fe Pixie ~
User 2063679 Photo


Registered User
22 posts

I've tried with setting sessions, but how do you set the session exactly and how do you retrieve and insert the data? If you would, please write the exact code
User 133269 Photo


Registered User
2,900 posts


<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
echo "$_SESSION['count']";
?>


you must have the session_start(); on every page....

http://nz.php.net/manual/en/session.examples.basic.php
Have fun
~ Fe Pixie ~

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.