SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My Sessions aren't working

    Hi, I am building a shopping cart where I send the item id and quantity via GET to my php page which then either creates a new session if one doesn't exist and adds the variables, or adds them to an existing session.

    I have an 'Orders' table which puts the 'order_number' in an auto_increment field and that becomes the SESSION['order_number'].
    Then another table Order_Item in which the order_number field isn't auto_incremented and each new 'add to cart' is supposed to add each item to this table with the users current SESSION['order_number'].

    All that happens is the first item gets put in the database but not any others.
    I've seen that no session file is being created.
    In my php.ini I have session.save_path = /tmp because I have access to the root of my 'virtual private server'. But no file is being created in there.
    I tried creating a directory in my http directory / public_html / sessions
    but that didn't work!

    Here is some of my code in the process cart page:
    <?
    session_start();
    include_once("function_cxn.inc");

    echo"<HTML>
    <HEAD>
    <TITLE>Item(s) added to cart</TITLE>
    </HEAD>
    <BODY>";

    $_SESSION['Quantity'] = $_GET['Quantity'];
    $Quantity = $_GET['Quantity'];
    $_SESSION['id'] = $_GET['id'];
    $id = $_GET['id'];

    if($Quantity=="")
    {echo"You haven't entered a quantity, please try again
    <p><form><input type=button value='Back' onclick=history.go(-1);return false; /></form>";
    exit;}

    if(!isset($_SESSION['order_number']))
    {$connect=connect_to_db("vars_o.inc");
    $today=date("d-m-Y");
    mysql_query("insert into Orders (date) values ('$today')");
    $order_number=mysql_insert_id($connect);
    $_SESSION['order_number']=$order_number;}
    else
    {$order_number=$_SESSION['order_number'];}

    Any help much appreciated, Steve

  2. #2
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    How are you passing the variables via get. Have you used the correct names and case.

    Also, instead of pulling in the get each time, use the following;

    PHP Code:
    $Quantity $_GET['Quantity'];
    $_SESSION['Quantity'] = $Quanitity;
    $id $_GET['id'];
    $_SESSION['id'] = $id

  3. #3
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    How are you passing the variables via get. Have you used the correct names and case.

    Also, instead of pulling in the get each time, use the following;

    PHP Code:
    $Quantity $_GET['Quantity'];
    $_SESSION['Quantity'] = $Quanitity;
    $id $_GET['id'];
    $_SESSION['id'] = $id

  4. #4
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by klik View Post
    Hi there,

    How are you passing the variables via get. Have you used the correct names and case.

    Also, instead of pulling in the get each time, use the following;

    PHP Code:
    $Quantity $_GET['Quantity'];
    $_SESSION['Quantity'] = $Quanitity;
    $id $_GET['id'];
    $_SESSION['id'] = $id
    Thanks for your reply.
    I tried your code above and it didn't work.
    In my item list page I put

    echo"<form action=added.php method=GET>";

    before the listings, then at the end of each line/product I put the quantity/add to cart

    echo"<td><font size=1>Qty<br>
    <input type=text name=Quantity size=2>
    <input type=hidden name=id value=$Picture>
    <input type=submit name=Products value=Add></font></td>
    </form>";

    As each item's $Picture is different I use that as the id.
    The values are being passed to my 'added.php' page as I echo the item details and the quantity on the page, along with the auto_incremented 'order_number'. The only thing that isn't working is the sesssions as only the first item added is registered in my database.

    Steve

  5. #5
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you have multiple items in your form, you need to make Quantity an array when the form gets posted (why use get for this?). There's no need for a hidden "id" input:
    PHP Code:
    /* I'm assuming the items are printed in a loop here */
    print "<input type='text' name='Quantity[".$Picture."]' size=2>"
    Do a print_r($_POST) on the processing page to see what that gives.
    In any case: since you have multiple item-quantity fields that all have the same name ("Quantity"), only one is going to be passed on to the receiving page. Perhaps show your form code?

  6. #6
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mudshark View Post
    Since you have multiple items in your form, you need to make Quantity an array when the form gets posted (why use get for this?). There's no need for a hidden "id" input:
    PHP Code:
    /* I'm assuming the items are printed in a loop here */
    print "<input type='text' name='Quantity[".$Picture."]' size=2>"
    Do a print_r($_POST) on the processing page to see what that gives.
    In any case: since you have multiple item-quantity fields that all have the same name ("Quantity"), only one is going to be passed on to the receiving page. Perhaps show your form code?

    I'm not sure whether Quantity needs to be an array. Only one item is posted (or got!) at one time and then the users clicks 'continue shopping' to select another item.
    I have based my coding on the book PHP SQL Everyday Apps for Dummies and she doesn't use an array.

    Here is the whole processing program to see if more light can be shed on my problem (what happens is only the first item is inserted into my 'Order_Item' databse and no Session file is created:

    <?
    session_start();
    include_once("function_cxn.inc");

    error_reporting(E_ALL);

    echo"<HTML>
    <HEAD>
    <TITLE>Item(s) added to cart</TITLE>
    </HEAD>
    <BODY>";

    $_SESSION['Quantity'] = $_GET['Quantity'];
    $Quantity = $_GET['Quantity'];
    $_SESSION['id'] = $_GET['id'];
    $id = $_GET['id'];

    if($Quantity=="")
    {echo"You haven't entered a quantity, please try again
    <p><form><input type=button value='Back' onclick=history.go(-1);return false; /></form>";
    exit;}

    if(!isset($_SESSION['order_number']))
    {$connect=connect_to_db("vars_o.inc");
    $today=date("d-m-Y");
    mysql_query("insert into Orders (date) values ('$today')");
    $order_number=mysql_insert_id($connect);
    $_SESSION['order_number']=$order_number;}
    else
    {$order_number=$_SESSION['order_number'];}

    connect_to_db("vars_p.inc");

    $query="select * from Beads where Picture = '$id'";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    extract($row);

    connect_to_db("vars_o.inc");

    mysql_query("INSERT INTO Order_Item (order_number,Stone,sub_category,Shape,Size,quantity,price)
    VALUES ('{$_SESSION['order_number']}','$Stone','$sub_category','$Shape','$Size','$Quantity','$Price')");

    echo "$order_number: $Quantity</b> x $Stone $sub_category $Shape $Size @ <b>&#163;$Price</b>";
    if($Quantity==1)
    {echo" has";}
    else
    {echo" have";}
    echo" been added to your cart
    <table>
    <td><tr>
    <form><input type=button value='Continue Shopping' onclick=history.go(-1);return false; /></form>
    </td>

    <td><form action=ShoppingCart.php method=POST>
    <input type=submit name=Cart value='View Shopping Cart'></form></td></tr>";

    ?>
    </BODY>
    </HTML>

  7. #7
    &lt;!-- Insert thoughts here --&gt; pitcher17's Avatar
    Join Date
    Apr 2004
    Location
    The great white north
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_SESSION variables cannot be set once output for the page has been generated.

    As soon as you do an echo or a print or break from your <?php ?> tags and write something that would be considered text (even whitespace) then php considers the headers for the page complete.

    After that point you can no longer set headers, cookies or session variables.
    The more time I save by not planning and documenting,
    the more time I have left to debug.


  8. #8
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I've done it!!

    Al it was was my 'order_number' field was a PRIMARY KEY and that does not allow the same value to be entered twice!
    So it only allowed the first item to be entered.

    So by making it not a primary key has worked!!!
    Anyone remember this in future if you have a similar problem.

    It's strange because the PHP & MySQL Everyday Apps for Dummies book tells you to make order_number a primary key!

    Thanks for previous help.
    Steve

  9. #9
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pitcher17 View Post
    $_SESSION variables cannot be set once output for the page has been generated.

    As soon as you do an echo or a print or break from your <?php ?> tags and write something that would be considered text (even whitespace) then php considers the headers for the page complete.

    After that point you can no longer set headers, cookies or session variables.
    This applies to Headers and cookies, BUT NOT session variables, they can be used like normal variables and set at any point of your code.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdee View Post
    OK I've done it!!

    Al it was was my 'order_number' field was a PRIMARY KEY and that does not allow the same value to be entered twice!
    So it only allowed the first item to be entered.
    In which table, your orders table must be a PK, but the order item must not be PK as you can have multiple items with the same order no
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  11. #11
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandes View Post
    In which table, your orders table must be a PK, but the order item must not be PK as you can have multiple items with the same order no
    I had the primary key in my 'order_item' table which was preventing more than one item with the same order number being entered.
    But I was was just following what I read in the book...

  12. #12
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdee View Post
    I had the primary key in my 'order_item' table which was preventing more than one item with the same order number being entered.
    But I was was just following what I read in the book...
    No I think youve confused yourself the Order_Item table cannot be a PK field, hence you said in your first post

    Then another table Order_Item in which the order_number field isn't auto_incremented
    The reason its not auto_inc, is that you can havve more than one entry with the same order no, understanding this is elementry to understanding how your database operates in this example, if you want a further explaination just ask
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •