Jump to content

[SOLVED] Session variable not sticking


tlavelle

Recommended Posts

This file has 3 forms in it.

 

Form 1 - presents to all users.  Users select a year and submit

Form 2 - Is conditional.  It only shows if users choose the value '2007'

Form 3 - Should present to all users

 

For whatever reason, my session variable for year seems to get lost after a user who selects 2007 in the first form submits the second form.  Help?

 

 

<?php
//initialize session
session_start();
?>

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php


// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "wercbench";


// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create form 1 queries
$year_query = "SELECT distinct year FROM bench_data";



// execute form 1 queries
$year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error());


// grab distinct years from db and stick them in the drop down
if (mysql_num_rows($year_result) > 0){

if (!isset($_POST['wercbench1']) and !isset($_POST['year']) || !isset($_POST['wercbench2']) and !isset($_POST['year'])) {

	echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench1\">";
	echo"<table style=\"width: 300;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
	echo"<tbody>";
	echo" <tr>";
	echo"   <td>Select the comparison year and then click next";
	echo"   </td>";
	echo"   <td></td>";
	echo" </tr>";
	echo" <tr>";
	echo"  <td>";
	echo"   <select name=\"year\">";
		while($row = mysql_fetch_row($year_result)) {
			echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";
		}
	echo"   </select>";
	echo"   </td>";
	echo"   <td></td>";
	echo"	</tr><tr>";
	echo"   <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>";
	echo" </tr>";
	echo" </form>";
}


$_SESSION['sess_year']=$_POST['year'];
	echo $_SESSION['sess_year'];


//if 2005 is selected just show the data
if(isset($_POST['wercbench1_next']) && ($_SESSION['sess_year']!='2005')){

		$industry_result = mysql_query("SELECT distinct comp_desc FROM comp_desc") or die(mysql_error());

		echo" <tr>";
		echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench2\">";
			echo"   <td>";
			echo"   <select name=\"industry\">";
				while($row = mysql_fetch_row($industry_result)) {
					echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";
				}
				echo"   </select>";
				echo"</td>";
				echo"   <td></td>";
				echo" </tr>";
				echo" <tr>";
				echo"   <td><input type=\"Submit\" value=\"Next\" name=\"wercbench2_next\"></button></td>";
				echo"   <td></td>";
				echo" </tr>";

				echo"</tbody>";
				echo"</table>";
				echo"</form>";


        
}


$_SESSION['sess_industry']=$_POST['industry'];


}

else {
    // no
    // print status message
    echo "No rows found!";


}


if(
(isset($_POST['wercbench2_next']) and ($_SESSION['sess_year']=='2007')) or (isset($_POST['wercbench1_next']) and ($_SESSION['sess_year']=='2005'))
) {


$useryear=$_SESSION['sess_year'];
//$_SESSION['sess_year']=$_POST['year'];
$userindustry=$_SESSION['sess_industry'];
//$_SESSION['sess_ind']=$_POST['industry'];
$metric_query = "SELECT metric_desc.met_desc, bench_data.median, bench_data.best_pract FROM metric_desc, bench_data where metric_desc.met_key=bench_data.met_key and $useryear=bench_data.year";
$metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error());

echo $_SESSION['sess_year'];
echo $_SESSION['sess_industry'];


echo "<form action=\"results.php\" method=\"post\" name\"wercbench3\">";
    echo "<table cellpadding=10 border=1>";
    echo "<tr>";
        echo "<td>Metric Description</td>";
        echo "<td>Average</td>";
        echo "<td>Best Practice</td>";
	echo "<td>Enter your value here</td>";
        echo "</tr>";
    while($row = mysql_fetch_row($metric_result)) {
        echo "<tr>";
        echo "<td>".$row[0]."</td>";
        echo "<td>" . $row[1]."</td>";
        echo "<td>".$row[2]."</td>";
	echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>";
        echo "</tr>";
    }
    echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"wercbench3\" value=\"Compare\"></td></tr></table>";
//}

// free result set memory
mysql_free_result($year_result);
//mysql_free_result($industry_result);
mysql_free_result($metric_result);
//mysql_free_result($metric_result);


}


// close connection
mysql_close($connection);

?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/68408-solved-session-variable-not-sticking/
Share on other sites

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.