Page 1 of 3
how to use session()
Posted: Sat Mar 07, 2009 12:07 pm
by nishmgopal
Hi guys
I am trying to pass variables across various pages and I believe sessions is the best way, but Im afraid I keep getting the following error on the page where my session starts:
Parse error: syntax error, unexpected T_VARIABLE
the code for this page is:
Code: Select all
<? session_start()
$getjob = $_SESSION['job'];
?>
I am assiging the variable $getjob with the form and all the ID's and the names are matching - job.
And on all the other pages I am using the following code:
Code: Select all
<? session_start()
$_SESSION['job'] = $_POST['job'];
?>
Can anyone help me? I would like to echo $_POST ['job'] on various pages.
Thanks
Re: how to use session()
Posted: Sat Mar 07, 2009 12:19 pm
by califdon
First, never use the "short" PHP opening tag. In other words, always use <?php.
Second, you're missing the semicolon at the end of session_start(); That causes the next line to be read as part of the first line, which causes the parser to see the variable $getjob or $_SESSION where it's not expecting a variable, which is what the error message said.
Re: how to use session()
Posted: Sat Mar 07, 2009 12:38 pm
by nishmgopal
thanks for that. But I still cant seem to echo it on other pages this is my code on one of the other pages I want to use 'job' in:
top of the page:
Code: Select all
<?php session_start();
$_SESSION['job'] = $_POST['job'];
?>
<h1 class="title2">Find Suitable Candidate: <?php echo $_POST['job'] ?></h1>
<?php
Then I have an sql statement on the same page:
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$_POST['job']";
What am i doing wrong?
Re: how to use session()
Posted: Sat Mar 07, 2009 1:37 pm
by jayshields
I trust that on the other pages you are also calling session_start() at the top and referencing job with $_SESSION['job'] and not $_POST['job'].
In the example you posted you are getting into some trouble with the quotes. You missed a single quote out of the SQL query and you also tried to use single quotes inside single quotes. In other words, change this:
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$_POST['job']";
to this:
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . $_POST['job'] . "'";
I would strongly recommend that you use mysql_real_escape_string() with every variable which you are passing from the user into a database, which would result in this:
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_POST['job']) . "'";
Re: how to use session()
Posted: Sat Mar 07, 2009 1:46 pm
by nishmgopal
the sql is still returning a blank screen, with no errors. The top of my page looks like this:
Code: Select all
<? session_start();
$getjob = $_SESSION['job'];
?>
Re: how to use session()
Posted: Sat Mar 07, 2009 6:49 pm
by califdon
Although it's probably not the problem, you are not following instructions very well. You are still using the short tag.
this is my code on one of the other pages I want to use 'job' in:
Well, that's
assigning a value to the session variable, not reading it.
Re: how to use session()
Posted: Mon Mar 09, 2009 6:19 am
by nishmgopal
Sorry I have changed my code to the long tag.
But How do I read the value job?
Re: how to use session()
Posted: Mon Mar 09, 2009 6:41 am
by JeffG
Re: how to use session()
Posted: Mon Mar 09, 2009 6:48 am
by nishmgopal
I tried that already,
I had:
Code: Select all
<? php session_start();
$getjob = $_SESSION['job'];
?>
But again all I get is a blank screen

Re: how to use session()
Posted: Mon Mar 09, 2009 6:57 am
by JeffG
There is really not enough information to begin to understand your problem. Perhaps if you showed the <form> statement with the action and method and the part of the form body where the value of 'job' is set it would help.
Also your first post looks as though it's the wrong way round. Do you really have multiple pages with forms setting the job variable, and a single page getting the value from the session variable?
Re: how to use session()
Posted: Mon Mar 09, 2009 6:58 am
by susrisha
seeing the above topics i thought i would add:
If you are trying to get the variable 'job' from the post of a form.
Code: Select all
$job=$_POST['job']; //lets say this is page1.php
If you want the variable job to be used in the same page
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$job'";
If you want to use the variable in another page(say page2.php)
Code: Select all
<?php
session_start();
$_SESSION['job'] = $_POST['job']; //write this in page1.php
?>
Code: Select all
<?php
session_start();
//write this in page2.php
$job = $_SESSION['job'];
//all further mysql queries
?>
Re: how to use session()
Posted: Mon Mar 09, 2009 7:11 am
by nishmgopal
I have done all the above but all I keep getting is a blank screen. The full code is broken down below:
Page 1, where the form is held:
Code: Select all
<?php session_start();
$_SESSION ['job'] = $_POST['job'];
?>
<?php
$conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql');
$dbname="conmg";
$dbname=@mysql_select_db($dbname, $conn)
or die("Couldn't select database");
////
$query = "SELECT * FROM Job_Spec";
$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='go.php'>
<p>
<label>
<select name='job' id='job'>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$Project_Name= $row['Project_Name'];
echo "<option value=\"$Project_Name\">$Project_Name</option>";
}
echo "</select></label></p>";
?>
Page 2 - Where I am trying to use the variable in a sql statement:
Code: Select all
<?
session_start();
$getjob = $_SESSION['job'];
?>
<?php
$conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql');
$dbname="conmg";
$dbname=@mysql_select_db($dbname, $conn)
or die("Couldn't select database");
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
$result1 = mysql_query($query1)
or die ("Couldn't execute query.");
.....
In the sql statement, if i replace $getjob with an actual name ie Manager the query works fine.
Re: how to use session()
Posted: Mon Mar 09, 2009 7:17 am
by susrisha
in page 1
Code: Select all
if(isset($_POST['job']))
{
$_SESSION ['job'] = $_POST['job'];
}
in page 2
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
echo "$query1";//just to check the actual query being passed.
$result1 = mysql_query($query1)
or die ("Couldn't execute query mysql_error()"); //check whats the error before exiting
Re: how to use session()
Posted: Mon Mar 09, 2009 7:24 am
by nishmgopal
susrisha wrote:in page 1
Code: Select all
if(isset($_POST['job']))
{
$_SESSION ['job'] = $_POST['job'];
}
in page 2
Code: Select all
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'";
echo "$query1";//just to check the actual query being passed.
$result1 = mysql_query($query1)
or die ("Couldn't execute query mysql_error()"); //check whats the error before exiting
I tried that and it echos :
$query1 = "SELECT * FROM Job_Spec WHERE Project_Name =''
Then I try and runt the query and its back to the blank screen!
Re: how to use session()
Posted: Mon Mar 09, 2009 7:27 am
by JeffG
Couple of assumptions I'm making:
- Somewhere lower down Page 1 you have a submit button and then close off the form.
- Page 1 itself is 'go.php', otherwise the assignment to the session variable will only take place when Page 1 is first executed, and not when the form is submitted and the post actually has something in it.