Jump to content

Session() help needed...please


nishmgopal

Recommended Posts

HI guys, I am assigning my session like this:

 

$sql="SELECT * FROM Job_ID WHERE Job_Name='$projectname'";
$result = mysql_query($sql) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))

$Job_ID=$row['Job_ID'];
{
if(isset($_POST['Job_ID']))
   {
    $_SESSION['Job_ID']=$Job_ID; 
	}
}
[code]

and at the top of this page I have session_start();.

On the other pages, where I want to use this job id variable I have got:

[code]
  <?php

session_start();
if(isset( $_SESSION['Job_ID']))
{
  $_SESSION['Job_ID']=$Job_ID;
}
echo $_SESSION['Job_ID'];

?>

 

But it doesnt echo anything and when I try to use the $_SESSION['Job_ID'] variable nothing is returned.  Can someone please help.

Link to comment
https://forums.phpfreaks.com/topic/149846-session-help-neededplease/
Share on other sites

I think it should be this:

 

$sql="SELECT * FROM Job_ID WHERE Job_Name='$projectname'";
$result = mysql_query($sql) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))
{

$Job_ID=$row['Job_ID'];

if(isset($_POST['Job_ID']))
   {
    $_SESSION['Job_ID']=$Job_ID; 
	}
}

 

You had the $Job_ID outside of the while loop.

I think it should be this:

 

$sql="SELECT * FROM Job_ID WHERE Job_Name='$projectname'";
$result = mysql_query($sql) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result))
{

$Job_ID=$row['Job_ID'];

if(isset($_POST['Job_ID']))
   {
    $_SESSION['Job_ID']=$Job_ID; 
	}
}

 

You had the $Job_ID outside of the while loop.

 

i tried this got and got nothing again...and yes i do have session_start at the start of each page

Wait a second. Do you have a table named Job_ID and a field named Job_ID? It looks like you are trying to one from a table and one from a field.

 

edit: wolfrage is correct too. sorry i didnt notice it was 2 pages since the code tags were messed up. it should be:

 

<?php
session_start();
if(!isset( $_SESSION['Job_ID']))
{
  $_SESSION['Job_ID']=$Job_ID; //Here is where you set your session var to a null variable.
}
echo $_SESSION['Job_ID'];
?>

<?php
session_start();
$sql="SELECT * FROM Job_ID WHERE Job_Name='$projectname'"; # The table and column name the same? Job_ID?
$result = mysql_query($sql) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result)) {
  $Job_ID = $row['Job_ID'];
  if(isset($_POST['Job_ID'])) {
        $_SESSION['Job_ID'] = $Job_ID; 
    }
}

 

Other page:

  <?php
session_start();

if(isset( $_SESSION['Job_ID']))
{
  $_SESSION['Job_ID'] = $Job_ID; # is $Job_ID set on this other page?
}
echo $_SESSION['Job_ID'];

?>

That was my mistake, the table name should have been Job_Table

 

However i made the changes and still not getting anything.  This is the code now:

 

<?php 
session_start();

$sql="SELECT * FROM Job_Table WHERE Job_Name='$projectname'";
$result = mysql_query($sql) or die ("Couldn't execute query.");

while ($row=mysql_fetch_array($result)) {
  $Job_ID = $row['Job_ID'];
  if(isset($_POST['Job_ID'])) {
        $_SESSION['Job_ID'] = $Job_ID; 
    }
}

 

and the other page:

 

<?php


session_start();

if(isset( $_SESSION['Job_ID']))
{
  $_SESSION['Job_ID'];
}
echo $_SESSION['Job_ID'];

?>

 

still no luck on the echo

Is $projectname set properly?

put echo $sql; just after the sql statement and post the result here

and whats the value of $_POST['Job_ID'] ?

 

<?php
$sql="SELECT * FROM Job_Table WHERE Job_Name='$projectname'";
echo $sql . "<br />";
echo $_POST['Job_ID'];
$result = mysql_query($sql) or die ("Couldn't execute query.");
?>

Also why even have this

<?php
if(isset( $_SESSION['Job_ID']))
{
  $_SESSION['Job_ID'];
}
?>

or at least change it to something meaningful like:

<?php
if(isset($_SESSION['Job_ID']))
{
  echo $_SESSION['Job_ID'];
}
else { echo 'Job ID Not Set.';}
?>

echo echo echo ......

yeah I would try what iarp said to do but with the second page you posted the if statement there is not need because you arent actually doing anything with it.

nvm....

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.