Not able to pass $_Session variables

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 2244 Views - Last Post: 29 January 2014 - 02:34 AM Rate Topic: -----

#1 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Not able to pass $_Session variables

Posted 28 January 2014 - 11:19 AM

Hello, I need help on passing $_SESSION variables from page to page. I have to pass from AddToCart.php to shop.php to basket.

<?php
   session_start();
   // get the product id
   $DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
   $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";
   $Quantity = isset($_GET['Quantity']) ? $_GET['Quantity'] : "";
   require "connect.php";
   $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";
   $stmt = $dbhandle->prepare($query);
   $stmt->execute();
   
   /* 
    * check if the 'cart' session array was created
    * if it is NOT, create the 'cart' session array
 . */
   if(!isset($_SESSION['cart'])){
     $_SESSION['cart'] = array();
     
    }else{
    array_push($_SESSION['cart'][] = $DVDID);
    // redirect to product list and tell the user it was added to cart
    header('Location: shop.php?$action=add&DVDID' . $DVDID . '&NameOfTheDVD=' . $name);
    }
?>


I have to pass them from here to here

<?php
	 
	
	  $action = isset($_GET['action']) ? $_GET['action'] : "";
	 
      $name = isset($_GET['NameOfTheDVD']) ? ($_GET['NameOfTheDVD']): "";
	  $DVDID = isset($_GET['DVDID']) ? ($_GET['DVDID']) : "";
	  $Quantity =isset($_GET['Quantity']) ? ($_GET['Quantity']) : "";
	  if($action=='add'){
		 echo "<h3>" . $DVDID . " was added to your cart.</h3>";
     }
	  if($action=='exists'){
    echo "<h3>" . $_GET($DVDID) . " already exists in your cart.</h3>";
}
     require "connect.php";
  
     $query = "SELECT `DVDID`, `NameOfTheDVD`, `Quantity` FROM `DVD`";
     $stmt = $dbhandle->prepare($query);
     $stmt->execute();
	 
	 $num = $stmt->rowCount();
	 
	 if($num>0){
      echo "<table border='3' cellpadding='10' position='relative` bottom= '450px';>";//start table
    
        // our table heading
            echo "<tr>";
            echo "<th class='textAlignLeft'>DVD Name</th>";
			echo "<th class='textAlignLeft'>DVD ID</th>";
			echo "<th>Quantity</th>";
            echo "<th>Action</th>";
            echo "</tr>";
        
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
           
            
            //creating new table row per record
                echo "<tr>";
				
                echo "<td>" . $row['NameOfTheDVD'] . "</td>";
				echo "<td>" . $row['DVDID'] . "</td>";
                echo "<td>" . $row['Quantity'] . "</td>";
                echo "<td class='textAlignCenter'>";
                    echo "<a href='AddToCart.php?DVDID={$DVDID}&name={$name}' class='customButton'>";
                        echo "<img src='Add.png' title='Add To Cart' />";
                    echo "</a>";
                echo "</td>";
            echo "</tr>";
			

        }
        
    echo "</table>";
        }

        // no products in the database
        else{
          echo "No products found.";
        }
		
		var_dump($_GET);

     ?>


I tried some debugging code such as var_dump and the $SESSION Variable $DVDID, $name and $Quantity are empty. SO help? Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Not able to pass $_Session variables

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 11:29 AM

You need to start a session at the beginning of the new script and then use the $_SESSION array in that script.
Was This Post Helpful? 0
  • +
  • -

#3 huzi8t9   User is offline

  • D.I.C Regular
  • member icon

Reputation: 25
  • View blog
  • Posts: 374
  • Joined: 11-July 07

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 11:31 AM

There's just a method I, personally, wouldn't use.

if(!isset($_SESSION['cart'])){
     $_SESSION['cart'] = array();
}else{
     //array_push($_SESSION['cart'][] = $DVDID);
     // instead of pushing the array, just assign it.
     $_SESSION['cart'][] = $DVDID;
     // redirect to product list and tell the user it was added to cart
     header('Location: shop.php?$action=add&DVDID' . $DVDID . '&NameOfTheDVD=' . $name);
}



As commented, I wouldn't use array_push, I'd personally just assign it a value, as I've altered your code. Also, where you're receiving the code, you also need a session_start().

Give it a go and let us know the results.

Hope this helps

~huzi

This post has been edited by andrewsw: 28 January 2014 - 11:46 AM
Reason for edit:: Removed huge, unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 11:36 AM

Hello, Thanks for replying me and trying to help me. It is very much appriciated.

It does not have any effect on the code/
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 11:55 AM

Did you follow CTphpnwb's advice? In particular, your second page doesn't attempt to read any session-data.

Your description, though, seems to indicate a mis-understanding of how sessions works. You don't pass session-data between pages, the data is available (shared) throughout the current session.
Was This Post Helpful? 0
  • +
  • -

#6 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:02 PM

Yes, I opened a session int he second page as well, I am just looking at the URL bit where the parameters are being passed
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:08 PM

So you are no longer talking about session-data..? :whistling:

Check what appears in the address-bar when you click your links. But in particular:
header('Location: shop.php?$action=add&DVDID' .

you have a dollar-sign in front of the word action.

This post has been edited by andrewsw: 28 January 2014 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:08 PM

I tried this line echo "<td> . $_GET["DVDID"]. </td>"; and I got this error

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING

I was reading this article and this is how "<td> . $_GET["DVDID"]. </td>"; or something similiar to this
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:11 PM

Quote

or something similiar to this

It is not something similar, it needs to be exact.
"<td>" . $_GET["DVDID"] . "</td>";

or
"<td>{$_GET['DVDID']}</td>";

This post has been edited by andrewsw: 28 January 2014 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#10 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:22 PM

I am getting this error Undefined Index for DVDID but I have defined the variable, when I try this statement

"<td>{$_GET['DVDID']}</td>";
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:33 PM

You are also missing an equals sign after DVDID.

In this line:
header('Location: shop.php?$action=add&DVDID' . $DVDID . '&NameOfTheDVD=' . $name);

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:34 PM

View Postmfredy92, on 28 January 2014 - 02:22 PM, said:

I am getting this error Undefined Index for DVDID but I have defined the variable...

No, you haven't. See post # 7.
Was This Post Helpful? 0
  • +
  • -

#13 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:36 PM

It is not making any difference.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:38 PM

View Postmfredy92, on 28 January 2014 - 02:36 PM, said:

It is not making any difference.

With posts like that it's not going to. You need to start thinking about the problem and what information might be required to solve it. The act of posting that information as part of your questions will help you learn. Plus, it will help us help you.
Was This Post Helpful? 0
  • +
  • -

#15 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not able to pass $_Session variables

Posted 28 January 2014 - 12:48 PM

I don't know what part of post 7 makes it interesting because Andrew asked me to remove the $ sign before action, which I did. According to you, DVDID is not defined, but I have defined it in the shop page and the AddToCart.

Yes, I did follow your instructions which was to use the function session_start() on all the scripts which I did. I think it is very important for you to know that while I have been stuck ion this problem for 2 weeks, I have been making progress as I was able to track the error and today thanks to your expertise in PHP, I have realised there is a problem in the URL.

Now with the URL, can you be sepcific on what I am doign wrong. Is it because I am not using the variables correctly or what?

Thank you for you advice.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3