Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Session Variable from Query 1

Status
Not open for further replies.

andy98

Programmer
Jul 7, 2000
120
GB
Hi
I have the following code and what I am trying to do is to set a SESSION variable from my QUERY for sales_person which also resides in the SALES table.

However, when I test for the existence of the variable elsewhere it doesn't seem to have been set. Have I got incorrect syntax for setting the variable?

Code:
<?
session_start();
include("conn.php");  

$s_password = $_POST['s_password'];
$s_username = $_POST['s_username'];
 
$query = "select * from sales 
WHERE s_username = '$s_username'
AND s_password   = '$s_password'";

$result = mysql_query($query) or die (mysql_error()); 

if(mysql_numrows($result) < 1)
{
      include("HeaderLogin.htm");
}

else
{
		session_start(); 
		$sales_person = $result['sales_person'];
		$_SESSION['Logged_In'] = "True";
		$_SESSION['s_password'] = $s_password; 
		$_SESSION['sales_person'] = $sales_person; 
		

echo "<script>document.location.href='listaccessorders.php'</script>";
}

?>

 
You don't need to have the session_start(); a second time in the if() statement, but other than that, the code looks fine to me. Are you sure the session is started on the page pulling the value? If so, put phpinfo(); in a new line somewhere after the session_start(); in that file and scroll to the bottom. Look in the table of php variables to see if it's set there or not.

Rick

 
Hi Rick

Can't see any of my variables with PHPINFO(); but I am outputting my
Code:
$_SESSION['s_password'];
successfully, but not the
Code:
$_SESSION['sales_person'];

 
<? echo $_SESSION['sales_person'] ?>
on the page you're setting it on, somewhere at the bottom. That'll tell you if it's set correctly or not. If it's not, <? echo $sales_person ?> to see if that's set to anything. Make sure you're pulling a value from the db.

Rick

 
Use this to set your variable:

Code:
<?
$sales_person = mysql_result($result,0,sales_person);
// or this? it's 5:30am, and it's been a long night...my mind's shut down....
//$sales_person = mysql_result($result,0,'sales_person');
?>



Rick

 
It worked!

Code:
$sales_person = mysql_result($result,0,'sales_person');

Rick your a STAR - so have one as well!!!

Many thanks, now go and get some sleep man!

 
Awesome! I've got more work to do still, though. At least it's enjoyable, easy stuff. I'm coding some stuff for a big promo I'm starting next week. And I'm finding some features in templatemonster's affiliate system that I never knew existed. It's giving me so much more control and making this system so much easier to make. Anyway, good luck with those sessions :-D.

GB,

Rick

 
Rick

weird this!

I am able to display the correct sales_person variable now - but when I access an order form where I can add a new order for an existing customer, I have a drop-down populated from the db that you can assign a sales_person to the order - which seems to change the session variable for the sales person who is logged in.

Here's the code for my drop down:
Code:
<?
$query4 = "SELECT * FROM sales ORDER BY sales_person ASC";
$result4 = mysql_query($query4) or die (mysql_error()); 

echo "<select name='sales_id' id='$sales_id'>";
   
     while ($row = mysql_fetch_row($result4))
	     {
                 $sales_id     = $row[0];
                 $sales_person = $row[1];
		 $sales_office = $row[2];
                 print("<option value=\"$sales_id\">$sales_person, $sales_office</option>\n");
         }
?>

And here's what the code used to display the sales_person and sales_office in the footer of each page whilst they are logged in:

Code:
echo "<br><font size='1'>You are Logged in as:</font><font color='blue' size='1'> " . $_SESSION['sales_person'] . " from the " . $_SESSION['sales_office'] . " Office</font>";

When I access this page with the drop-down, it changes the session variable for sales_person and sales_office that i originally set after the LOGIN.

Don't answer this now - go and get some sleep!



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top