Jump to content

a little sessions help please


mindapolis

Recommended Posts

Hi, I have been looking at this code for hours and I can't figure out why the product name and quantity aren't transferring from treats.php to checkOut.php.  I realize checkOut.php is extremely long but if I could get some help I would really appreciate it. 

 

 

In case you would want the link, it 's http://auntievics.com/treats.php

18199_.php

18200_.php

Link to comment
Share on other sites

In the top of treats I think you want to use this.  Obviously, you can't echo a value and then use the header, but I understand this is for testing.

<?php
session_start();
if(!isset($_SESSION['quantity']))
{
	$_SESSION['quantity']=array(); //if there are no quantities selected, the array is empty
}
if(isset($_POST['quantity']))//if there are items in the cart
{
	echo "{$_POST['quantity']}";
	header("location: checkOut.php");
}
require_once("functions.php");
?> 

 

Then on the check out page, session_start() should always be added.

<?php
session_start();


$fname="";
$lname="";
$username="";
//ect...

 

Link to comment
Share on other sites

The biggest problem is that you don't have a POST field named 'quantity'. You are making a series of POST fields named 'quantityID', where the ID is each product id.

 

You should instead use an array name for your field - name = 'quantity[ID]'

 

Once you use an array name, you can test if $_POST['quantity'] is set or if it is an array (currently you can do neither.)

Link to comment
Share on other sites

Daah.  Good call PFMaBiSmAd

Yes, when looping through products like this you'd have something like name="productid[]" value=" your php id value echoed" Same for quanitity, name="quantity[]".  These posted values will have the same array KEY, you can then use this key to grab all related data for this item.

Link to comment
Share on other sites

Once you change the quantity form field to use an array name, your $_POST array will end up looking like the following (the 123,124,23,24 are some made up product id's) -

Array
(
    [quantity] => Array
        (
            [123] => 1
            [124] => 3
            [23] => 2
            [24] => 1
        )

    [ProceedToCheckout] => Proceed to Checkout
)

 

Your echo statement(s) on the treats.php page would be -

 

	echo '<tr><td width="200px"><img src="'.$row['product_pic'].'" /></td><td width="200px"><b>'.$row['product_title'].'</b><br />'.$row['product_Description'].'<br /> Price:  $'.$row['price'].$row['pricePer'].'<br /><br />Quantity <input name="quantity['.$row["product_id"].']" type="text" size="2" /></td></tr>';

 

On your treats.php page, you should have all your products in ONE table with a product category/type column. You retrieve the products in the order that you want them and simply output a new heading when the category/type column changes. You will greatly simplify your code (only one query.)

 

Link to comment
Share on other sites

Further to the above (concerning using one table for all your products.) Your checkOut.php page queries against the `treats` table, but it has no queries against the `specialty_treats` table. There's no way your current checkOut.php page will work correctly if someone selects something form the specialty_treats table.

 

Use one table for all your products or you will be forever writing duplicate sections of code or forgetting to write them as the case may be.

 

You also have a hundred plus dog breads in a select menu. Why did you write out all that code? You should either have a database table with that list in it or at a minimum an array with the list. You would then have about three lines of php code to produce all the options.

Link to comment
Share on other sites

I have looked at your checkOut.php code further, and if your intent (per the title of this thread) is to store the cart contents (id/quantity) in a session variable, where's your code to do that? There's nothing in the posted code that stores any of the cart information in a session variable.

 

I recommend that you first define what you want to do, then try to write (and test) the code to do it.

Link to comment
Share on other sites

so the array would be. . . 

Array {
      [quantity]=>array 
         [
           [burgers]=>1  //List each product like this?  
} 

that would transfer the product name and the quantity over to the checkout page, right? 

Once you change the quantity form field to use an array name, your $_POST array will end up looking like the following (the 123,124,23,24 are some made up product id's) -

Array
(
    [quantity] => Array
        (
            [123] => 1
            [124] => 3
            [23] => 2
            [24] => 1
        )

    [ProceedToCheckout] => Proceed to Checkout
)

 

Your echo statement(s) on the treats.php page would be -

 

	echo '<tr><td width="200px"><img src="'.$row['product_pic'].'" /></td><td width="200px"><b>'.$row['product_title'].'</b><br />'.$row['product_Description'].'<br /> Price:  $'.$row['price'].$row['pricePer'].'<br /><br />Quantity <input name="quantity['.$row["product_id"].']" type="text" size="2" /></td></tr>';

 

On your treats.php page, you should have all your products in ONE table with a product category/type column. You retrieve the products in the order that you want them and simply output a new heading when the category/type column changes. You will greatly simplify your code (only one query.)

Link to comment
Share on other sites

is there an easy way to copy fields from one table to another? 

 

Further to the above (concerning using one table for all your products.) Your checkOut.php page queries against the `treats` table, but it has no queries against the `specialty_treats` table. There's no way your current checkOut.php page will work correctly if someone selects something form the specialty_treats table.

 

Use one table for all your products or you will be forever writing duplicate sections of code or forgetting to write them as the case may be.

 

You also have a hundred plus dog breads in a select menu. Why did you write out all that code? You should either have a database table with that list in it or at a minimum an array with the list. You would then have about three lines of php code to produce all the options.

Link to comment
Share on other sites

that would transfer the product name and the quantity over to the checkout page, right? 

 

Yes, that would cause the quantity for each product_id to be submitted in an easy to use format that you can test for and process. There's also no need to loop through the entire contents of your database table in the checkout.php page. You can simply filter the quantities that are not empty and then directly query for just the matching rows from your database table.

 

I would also hope that your product_id's are the auto-increment index values (will result in the least amount of data and the quickest queries.)

Link to comment
Share on other sites

is there an easy way to copy fields from one table to another? 

 

If you mean copy rows from one table to another, yes, you would use an INSERT ... SELECT query -

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [iGNORE]

    [iNTO] tbl_name [(col_name,...)]

    SELECT ...

    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

 

 

Link to comment
Share on other sites

on treats.php where would I put this code? 

 

Once you change the quantity form field to use an array name, your $_POST array will end up looking like the following (the 123,124,23,24 are some made up product id's) -

Array
(
    [quantity] => Array
        (
            [123] => 1
            [124] => 3
            [23] => 2
            [24] => 1
        )

    [ProceedToCheckout] => Proceed to Checkout
)

 

Your echo statement(s) on the treats.php page would be -

 

	echo '<tr><td width="200px"><img src="'.$row['product_pic'].'" /></td><td width="200px"><b>'.$row['product_title'].'</b><br />'.$row['product_Description'].'<br /> Price:  $'.$row['price'].$row['pricePer'].'<br /><br />Quantity <input name="quantity['.$row["product_id"].']" type="text" size="2" /></td></tr>';

 

On your treats.php page, you should have all your products in ONE table with a product category/type column. You retrieve the products in the order that you want them and simply output a new heading when the category/type column changes. You will greatly simplify your code (only one query.)

Link to comment
Share on other sites

I put

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [iGNORE]
    [iNTO] treats [(product_id, product_title, product_Description, price, pricePer)]
    SELECT *; 

and I got

Error

 

SQL query: Documentation

 

INSERT [

LOW_PRIORITY |

HIGH_PRIORITY ][

IGNORE ][

INTO ]treats[(

product_id,

product_title,

product_Description,

price,

pricePer

)]

SELECT *

 

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[LOW_PRIORITY | HIGH_PRIORITY] [iGNORE]

is there an easy way to copy fields from one table to another? 

 

If you mean copy rows from one table to another, yes, you would use an INSERT ... SELECT query -

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [iGNORE]

    [iNTO] tbl_name [(col_name,...)]

    SELECT ...

    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Link to comment
Share on other sites

on treats.php where would I put this code?

Quote from: PFMaBiSmAd on Yesterday at 03:22:10 PM

 

 

    Once you change the quantity form field to use an array name, your $_POST array will end up looking like the following (the 123,124,23,24 are some made up product id's) -

    Code: [select]

 

    Array

    (

        [quantity] => Array

            (

                [123] => 1

                [124] => 3

                [23] => 2

                [24] => 1

            )

 

        [ProceedToCheckout] => Proceed to Checkout

    )

 

 

    Your echo statement(s) on the treats.php page would be -

 

    Code: [select]

 

    echo '<tr><td width="200px"><img src="'.$row['product_pic'].'" /></td><td width="200px"><b>'.$row['product_title'].'</b><br />'.$row['product_Description'].'<br /> Price:  $'.$row['price'].$row['pricePer'].'<br /><br />Quantity <input name="quantity['.$row["product_id"].']" type="text" size="2" /></td></tr>';

 

 

    On your treats.php page, you should have all your products in ONE table with a product category/type column. You retrieve the products in the order that you want them and simply output a new heading when the category/type column changes. You will greatly simplify your code (only one query.)

 

Report to moderator  184.18.231.73

Online mindapolisTopic starter

 

    Enthusiast

    Posts: 176

        View Profile

        Personal Message (Online)

 

Re: a little sessions help please

« Reply #13 on: Today at 10:27:28 AM »

 

    Quote

 

What 's wrong with this block of code?

 

Code: [select]

 

Array (

[quantity] => Array

(

[boChunkyPeanutButterChunchies] => 1

)

[ProceedToCheckout] => Proceed to Checkout

)

 

it doesn't like

Code: [select]

 

[quantity] => Array

 

What is it you need help with specifically that wasn't covered by PFMaBiSmAd earlier.

Link to comment
Share on other sites

I'm not sure it is possible to help you further with this. You don't seem to know your own code (to the point of not being able to identify where you would replaced lines in it with a statement that I modified for you) and seem to be lost over what it is, where to put it, or how to use most of the information that is being posted, which is probably why El Chupacodra posted his reply above.

 

So, to just recap, before I ignore this thread -

 

The session code you put into the start of the treats.php file doesn't actually do anything toward using session variables to hold the cart contents, because the treats.php page isn't POSTED to by a form and that code won't ever assign anything to the session variable (you also wouldn't blindly use external data without validating it first.) You also don't have any code anywhere else to use the data stored in the session variable. You seem to have hacked together and added a few lines of code to one of your files, posted your code in a forum, and hope someone can figure out what you are trying to accomplish and will fix it for you. Sorry, but we cannot figure out from that attempt what it is you are trying to accomplish and we aren't here to think through and write your whole application for you.

 

would there be anyway you could post that portion of the code.  I'm sorry, but I really struggle with this part of PHP
on treats.php where would I put this code?
 

 

^^^ Someone then did post the code you would need to change (i.e. Your echo statement(s) on the treats.php page would be..) and posted an example of the resulting data that would be submitted (i.e. your $_POST array will end up looking like the following...) Your treats.php code doesn't have that many lines of code in it and not many echo statements, but you didn't seem to get it that you are expected to take the supplied information and know enough about your own code to be able to apply that information to what you are doing. Once your treats.php page is submitting the $_POST data you expect, it is up to you to write the correspond code in your checkout.php page to use that data.

 

There's at least two replies of yours where you seem to think the example of "your $_POST array will end up looking like the following..." is something you put into your source code. I'm afraid not, but that only confirms that you don't understand what your own code is currently doing or that the form's submitted data will be received in the $_POST array in the checkout.php page.

 

Lastly, here is something I posted above -

I recommend that you first define what you want to do, then try to write (and test) the code to do it.

 

Programming is like writing a Math Word Problem and then solving that math word problem. You must have a clearly defined and stateable goal before you can write any code to accomplish that goal. This thread doesn't have any apparent goal, which is why it has not gotten very far, and I even questioned the intent, but got no reply -

 

... if your intent (per the title of this thread) is to store the cart contents (id/quantity) in a session variable ...

 

Before you work on your code further or ask for more help with your code, you need to define what you are trying to accomplish (I suspect it has something to do with storing the cart in a session variable so that your update/delete ajax code will have something to operate on) so that both you can write code that has meaning toward that goal and someone here will be able to post replies having something to do with achieving that stated goal.

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.