Jump to content

Problem with shopping cart made from mysql and php


derm116

Recommended Posts

Hi all,

 

Just wondering if anyone could please help me?? I'm writing a basic shopping cart function in php. I've created a databse in mysql containing a user, a cart and a book among other things.

I've used the following function to create a cookie

 

function GetCartID()

{

/* This function will generate an encrypted string and will set it as a cookie using set_cookie. This will also be used as the cookieID field in the cart table. */

 

  if(isset($_COOKIE["cartID"])) {

      return $_COOKIE["cartID"];

  }

  else {  /* There is no cookie set. We will set the cookie and return the value of the users session ID */

 

      session_start();

      setcookie("cartID", session_id(), time() + ((3600 * 24) * 30));

      return session_id();

  }

}

 

In my cart page then i set the users username and password to the cookie as follows

 

$username = $_COOKIE["user_derm116"];

$password = $_COOKIE["pw_derm116"];

 

with derm116 been the name of my database

My problem is when i am trying to add items to the cart, done as follows

 

if($numRows == 0) {

/* This item doesn't exist in the users cart, we will add it with an insert query */

$query = "INSERT INTO cart (cookieID, bookID, qty) VALUES ('" . GetCartID() . "', $bookID, $qty)";

@mysql_query($query) or die("Query failed");

}

 

The problem is, the cookie becomes a string when it is created but my userid is a auto-incrementing int, which means i can't connect the 2!!! I've tried loads of different ways to get over this problem but can't figure it out and its driving me mad!!!

I need to connect the user to the cart so that everytime they add an item to the cart it's saved to their cart.

 

Does anyone have any idea on how i could connect the user to the cart???

Thanks a mil in advance

 

Regards,

Derm

Link to comment
Share on other sites

Sorry, i'm even starting to confuse myself now.

What i want to do is to link my user table and my cart tables somehow.

Each session creates a unique cookie using the sessionId. I want to somehow have the cookie and user linked to the cart so that each time a user adds an item to the cart, the cart is changed to show the new item added to the cart.

For the remainder of the session the user should be able to check their cart and see all the items they've added so far.

To do this both the cookie and the user need to be linked to the cart???

Any ideas?

Hope thats a bit clearer,

tanx

Link to comment
Share on other sites

Databases should be in 3rd normal form, which in essence is what you are trying to get it.

 

I would suggest having an Auto_increment catid for the category table and than put the catid in the table that you want to link to.

 

Search for 3NF under SQL it should explain it some more.

Link to comment
Share on other sites

I think my tables are in 3NF already

For the 3tables been used here, book has a primary key bookid - int - auto-increment

                                            cart has a primary key cartid - int - auto-increment

                                            user has a primary key userid - int - auto-increment

Originally i had a foreign key called userid in my cart table which linked back to userid, and a foreign key in the cart table called bookid linking back to book, however on creating the cookie the userid becomes a string which can't link back to the int in the user table!!

Just can't figure it out!

any ideas on what i might be doing wrong??

Tanx a mil

Link to comment
Share on other sites

What you say does not really make sense. I understand that creating a cookie makes it "a string" but using the settype should fix that problem just call that settype on the cookie and it should convert it to int. than make your DB calls for that userid.

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.