Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:38:27+00:00 2026-06-17T23:38:27+00:00

I’m confused on how to do this. Say the user creates an account with

  • 0

I’m confused on how to do this. Say the user creates an account with my ecommerce website, and then starts adding products into their basket. If I store the users username and password in a database table, and use sessions/cookies to manage the products in their shopping basket, what would I need to do in order to connect the users shopping basket to their account, so that when they log in they will be able to see the items they had previously stored?

Do I first allow the user to login, query whether they logged in successfully, and then make a session/cookie variable for their username? Or do I have to store the users cart items in a database and connect it with the user accounts table?

I’m confused with how to store a shopping baskets items into a table. Is that even the right way to do it?

There’s no code yet, I want to create the databases correctly before I start coding and just need some advice. Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:38:28+00:00Added an answer on June 17, 2026 at 11:38 pm

    If you have two tables, one for the users and one for the items, you can do something like the following.

    Manage current basket items by adding to and removing the item_ids into a serialized array, which you can store in your users table AND the session at the same time, keeping them in sync. For example, if a user visits your store for the first time (not logged in, and empty shopping basket), you can create the session like so.

    session_start();
    

    We start the session.

    if (isset($_SESSION['current_basket']) {
        $current_basket = unserialize($_SESSION['current_basket']);
    } else {
        $current_basket = array();
    }
    

    Because this is the first time our visitor has visited our page, the session variable current_basket will not be set, meaning the above if statement will not run and instead just create an empty PHP array in a variable called current_basket.

    Now, when the visitor adds an item to the basket, we just need the item’s ID in your database and add it into the current_basket array.

    $current_basket[] = $item_id;
    

    Then we immediately update the session variable with the new array, serializing it in the process.

    $_SESSION['current_basket'] = serialize($current_basket);
    

    You now have a proper, usable array with all the product IDs for that person’s shopping basket.

    Now, let’s pretend the user was logged in. We’d check for that and only add one more step.

    $sql = "UPDATE users SET current_basket=" . serialize($current_basket) . " WHERE id=$user_id"
    // Execute that query.
    

    There, now that same array is in the database which you can then pull and set as a session variable when the user logs in.

    You can run these series of steps everytime a product is added or deleted, keeping it updated both in the session and the database.

    This is obvisouly a very watered down concept for the sake of explaining. Obviously, you’d need to manage the array better. For example, not adding a product to the basket if it’s already there, removing items from the array…etc.

    The key is to serialize the array, and store in the session always, and in the users table if the user is logged in.

    Then when the user comes back and logs in, you can set the array in the session for them using the array in the database from when they were last on your site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am confused How to use looping for Json response Array in another Array.
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.