Storing Variables In Session()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Storing Variables In Session()

Post by nishmgopal »

Hi Guys please refer to my code below:

Code: Select all

$query = "SELECT * FROM Job_ID";
    $result = mysql_query($query);
    
    echo "<h1 class='title2'>Upcoming Project Roles</h1>
    <p>From the menu below please select the Project Role:</p>
     
    <form id='form2' method='post' action='new_go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
[color=#BF0000] $Job_ID = $row['Job_ID'];[/color] - [color=#40FF00]HOW CAN I STORE THIS IN SESSION?[/color]
       $Project_Name= $row['Job_Name'];
       
       echo "<option value=\"$Project_Name\" \"$Job_ID\">$Job_ID - $Project_Name</option>";
   }
   
 echo "</select></label></p>
  <input type='submit' name='notjob' id='button' value='Go' />
 </label>
 </p>
 </form>
";
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Storing Variables In Session()

Post by nishmgopal »

can anyone please help?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Storing Variables In Session()

Post by mattpointblank »

Underneath it (before any output is displayed):

Code: Select all

 
session_start();
$_SESSION['Job_ID'] = $Job_ID;
 
You can now use this variable ($_SESSION['Job_ID']) on any page on your site, as long as you call session_start() beforehand. Really though, a quick search on Google or the PHP manual site would have told you this.
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Storing Variables In Session()

Post by nishmgopal »

I have already tried this and it didnt work. the top of my page where the variable is coming from looks like:

Code: Select all

<?php 
 
session_start();
$_SESSION['Job_ID'] = $Job_ID;
 
?>
and on everyother page where I want to use this variable looks like this:

Code: Select all

 <?php
 
 session_start();
$_SESSION['Job_ID']; 
 ?>
and to use this variable:

Code: Select all

<div class="title2"> <? echo $_SESSION['Job_ID'] ?> Specification:</div>
This doesnt work. What am i doing wrong?
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Storing Variables In Session()

Post by nishmgopal »

I have figured out the problem! Got it to work

But now I am having trouble callin the variable $Project_Name, this is what my code looks like:

Code: Select all

$Job_ID = $row['Job_ID'];
       session_start();
       $_SESSION['Job_ID'] = $Job_ID;
       $Project_Name= $row['Job_Name'];
       $_SESSION['Job_Name'] = $Project_Name;
I can call the $Job_ID by using $_SESSION['Job_ID'] = $Job_ID, but when I try to use $_SESSION['Job_Name'] = $Project_Name it doesnt work.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Storing Variables In Session()

Post by susrisha »

post any errors that you are getting and add an echo before assigning to session to see whats the value that you are getting in $Project_Name
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Storing Variables In Session()

Post by nishmgopal »

I think I should explain the whole system again, as even my $Job_ID is not working as I would want.

I have page 1 where a drop down menu is generated by my sql statement, the code looks like:

Code: Select all

$query = "SELECT * FROM Job_ID";
    $result = mysql_query($query);
    
    echo "<h1 class='title2'>Upcoming Project Roles</h1>
    <p>From the menu below please select the Project Role:</p>
     
    <form id='form2' method='post' action='new_go.php'>
   <p>
    <label>
 <select name='job' id='job'>";
    
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
       $Job_ID = $row['Job_ID'];
       session_start();
       $_SESSION['Job_ID'] = $Job_ID;
       $Project_Name= $row['Job_Name'];
 
       
       echo "<option value=\"$Project_Name\" \"$Job_ID\">$Job_ID - $Project_Name</option>";
   }
The on Submit, the action goes to page2, where I want to display all the records from the job table where the Job ID is passed from Page 1 ($Job_ID). At first I thought this was working, but when I select different records from the drop down menu and hit submit, the same value keeps showing. the code for page 2:

Code: Select all

 
        
     <?php
 
 session_start();
$_SESSION['Job_ID']; 
 ?>
 
$query1 = "SELECT * FROM Job_ID WHERE Job_ID ='$_SESSION[Job_ID]'";
            
  $result1 = mysql_query($query1)
       or die ("Couldn't execute query.");
 
while($row = mysql_fetch_assoc($result1)){
  $array[] = $row; }
     
array2table($array,200);
 
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

Re: Storing Variables In Session()

Post by nishmgopal »

please help!
Post Reply