Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Inserting Session data into a mysql database
Welcome, Guest. Please login or register.
December 02, 2008, 06:48:21 AM
11304 Posts in 1248 Topics by 498 Members
Latest Member: katCheeme
Experts Round Table Network  |  Databases  |  MySQL  |  Inserting Session data into a mysql database « previous next »
Pages: [1]
Author Topic: Inserting Session data into a mysql database  (Read 432 times)
paul_mellon36

Offline Offline

Posts: 17


WWW
« on: February 24, 2006, 01:08:54 PM »

Hi there,
OK, I'm afraid I'm just looking for abit of help to start on this one.
I have a session array containing an item id and an item quantity. I need to insert these values into a table called tblstore_orders.
I will post code below of which may show you a bit of what i have.
Code:

<?php
session_start();

if(!isset($_SESSION['basket']))
{
$_SESSION['basket'] = array();
}

if(isset($_POST['selected_item_id']))
{
$u_item_id = $_POST['selected_item_id'];
$u_quantity = $_POST['sel_item_qty'];
$u_details = $u_item_id."|".$u_quantity;
$_SESSION['basket'][] = $u_details;
header("Location: showcart.php");
}
?>
Logged

Paul
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #1 on: February 24, 2006, 03:15:04 PM »

hi

I don't see any problem. What you have seems correct so far, except that you don't test if the item_id submitted by POST is NOT ALREADY IN THE session BASKET. :D

In the block where you assign a new basket entry into the session veriable, establish your DB connection and insert the relevant values into your table. It's as simple as this.

There is also an other solution (in fact, there are many others) :
- you could change the session handler to not be files on the server but a DB handler. This way your baskets would be in the DB already.
- you could write down the session variable to a secondary file  (under the current web directory, like in ./baskets ), there are very nice functions for dumping an object to disk.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
paul_mellon36

Offline Offline

Posts: 17


WWW
« Reply #2 on: February 26, 2006, 12:21:12 PM »

:notworthy:
ok, i don't think you understand what i mean, i'm just looking for a few lines of code that would do the following using the sessions i have created above.

The session has 2 parts, an item_id and a quantity.
    1/ Get the item_id's out of the session as there will be more than 1 item is stored within it.
    2/ Query the database using that item_id - i can do this
    3/ Get the quantity of the corresponding item_id's out of the other part of the session and put this into the database along with the corresponding item_id also.
    4/ Hopefully this is all?
Logged

Paul
Pages: [1]
« previous next »
    Jump to: