Click to See Complete Forum and Search --> : session problem
mattgoody
01-01-2007, 05:28 PM
i use a form to display certain fields of a mysql db. i get the $_POST stuff in and check which isset and then display those fields. i also want to sort this data on any field the user chooses. so i store the POST variables into session variables, to check which fields isset after reloading the page with sorted results. this works if i sort the data once, but when i try to sort again (my sort works by putting a link on the column header: samepage.php?a=category) the session variables are not set anymore.
if (isset($_POST['Date2']))
{
$_SESSION['Date2'] = $_POST['Date2'];
$Date2 = $_SESSION['Date2'];
$_SESSION['numplayers'] = $_POST['numplayers'];
$numplayers = $_SESSION['numplayers'];
}
else
{
$Date2 = $_SESSION['Date2'];
$numplayers = $_SESSION['numplayers'];
}
thats my code thats setting the session variable if the form has been submitted, and just reading the session variables if the form hasnt been just submitted (which means that the sessions have already been set because the only way to get to this page is thru the form). so what im asking is why the session variable sets for one sort (page load) but not for the next. at the top of my code i have
session_start();
thanks
-Matt
chestertb
01-01-2007, 11:07 PM
It might be that you're using the same field names in both SESSION and POST.
I know that it shouldn't matter but I had an issue a few weeks back with something similar and the problem went away when I used different field names.
Cheers
CTB
NightShift58
01-01-2007, 11:13 PM
You mention receiving user data via a form and reading $_POST to extract that data.
Later in your description, you mention "putting a link on the column header". How do you retrieve that? At that point, it's no longer a $_POST but a $_GET. Could that be part of the problem?
You're not posting very much code, so it's hard to tell if somewhere else in the script something is taking place that shouldn't be.
chestertb
01-01-2007, 11:24 PM
Good point NightShift.
Instead of $_POST, try $_REQUEST, which will read the variable irrespective of whether it's a $_POST or a $_GET.
CTB
NightShift58
01-01-2007, 11:31 PM
If the $_POST/$_GET thing is the problem, $_REQUEST would fix it.
Personally, I'd prefer checking $_POST and $_GET specifically because of the little tiny bit of additional information that I would be getting, i.e.: Is the request coming from the <FORM> - which may imply certain actions - or from a link - which may lead to a different set of actions, in whole or parts.
Granted that in this particular case, differentiating between the two may have no bearing.
mattgoody
01-02-2007, 10:44 AM
no, the posts and gets arent being confused, although it is good to learn that request handles the both. ill post smoe more code and try to better explain whats going on.
<?php
session_start();l
?>
<?php
include("hot.css");
$db = mysql_connect('localhost', 'fantasyr_mattgoo', 'OOGA8319','true') or die('Could not connect: ' . mysql_error());
mysql_select_db("fantasyr_fantasyratings") or die(mysql_error());
if (isset($_POST['Date2']))
{
$_SESSION['Date2'] = $_POST['Date2'];
$Date2 = $_SESSION['Date2'];
$_SESSION['numplayers'] = $_POST['numplayers'];
$numplayers = $_SESSION['numplayers'];
}
else
{
$Date2 = $_SESSION['Date2'];
$numplayers = $_SESSION['numplayers'];
}
$sql = 'CREATE TEMPORARY TABLE TempTable1 (MIN int, ';
$sql2 = 'CREATE TEMPORARY TABLE TempTable2 (';
$sql3 = 'INSERT INTO TempTable1 SELECT '.$Date2.'.MIN, ';
$query = 'SELECT ';
$totalscore = '(0';
if (isset($_POST['FGM']) || isset($_SESSION['FGM'])) { $_SESSION['FGM'] = $_POST['FGM']; $stat = 'FGM'; $sql = $sql.$stat.' double,'; $sql2 = $sql2.$stat.'AVG double, '.$stat.'STD double,'; if (isset($_POST['AVG'])) {$sql3 = $sql3.$Date2.'.'.$stat.'/'.$Date2.'.GP,';} else {$sql3 = $sql3.$Date2.'.'.$stat.',';} $query = $query.total($stat).' As '.$stat.',' ; $totalscore = $totalscore.'+'.total($stat) ; }
if (isset($_POST['FGA']) || isset($_SESSION['FGA'])) { $_SESSION['FGA'] = $_POST['FGA']; $stat = 'FGA'; $sql = $sql.$stat.' double,'; $sql2 = $sql2.$stat.'AVG double, '.$stat.'STD double,'; if (isset($_POST['AVG'])) {$sql3 = $sql3.$Date2.'.'.$stat.'/'.$Date2.'.GP,';} else {$sql3 = $sql3.$Date2.'.'.$stat.',';} $query = $query.total($stat).' As '.$stat.',' ; $totalscore = $totalscore.'+'.total($stat) ; }
$totalscore = $totalscore.')';
$sql = $sql.' ID int) TYPE=HEAP;';
$sql2 = $sql2.' blank int) TYPE=HEAP;';
$sql3 = $sql3.'players.id FROM '. $Date2 .', players WHERE players.last = '. $Date2 .'.last AND players.first = '. $Date2 .'.first ORDER BY MIN DESC;';
$query = $query.' '.$totalscore.' AS Score, TempTable1.id, players.first, players.last FROM TempTable1, TempTable2, players WHERE TempTable1.id=players.id ORDER BY '.$_GET["a"].' DESC;';
$query2 = 'SELECT TempTable1.*, '.$totalscore.' AS Score, players.first, players.last FROM TempTable1, TempTable2, players WHERE TempTable1.id=players.id ORDER BY '.$_GET["a"].' DESC;';
basically what happens is that when i submit the form, it goes to page.php?a=Score so it first orders by score. then the first time i click on a column header (i didnt post the output code) the page reloads with ?a=(whatever header i clicked) and the thing works fine, which means that the session variable isset. but if i try to sort another column after that, none of the session variables register as set, and i get empty sql queries, and therefore no output.
-Matt
EDIT: i did actually search these forums and found something about making the POST and SESSION names different, which i tried, but the exact same error occurred. so i just put them back to having the same name.
NightShift58
01-02-2007, 12:06 PM
I think you may have a problem with:if (isset($_POST['FGM']) || isset($_SESSION['FGM'])) { $_SESSION['FGM'] = $_POST['FGM']; ... etc ...The basic condition you are checking is:IF either $_POST['FGM'] OR $_SESSION['FGM'] are set :
then overwrite $_SESSION['FGM'] with the value of $_POST['FGM']
ENDIF;What happens when _$POST['FGM'] isn't set? You "unset" your session variable with to an empty string. The first time around - coming from a $_POST - things will work out fine. The second time around - coming from a _$GET - it breaks... Same thing happens to 'FGA' on the next line...
I'm not sure that this is what leads to your overall problem due to the relative complexity of your code, but there's a "good" chance it might be...
mattgoody
01-02-2007, 12:10 PM
o man right, and that would explain why it works once and then not a 2nd time. i wish id spotted that error. thanks very much. is there a way to have it only do that is the $_POST variable isset? like rather than doing an if elseif where id have to write out the consequences of the if for both cases
NightShift58
01-02-2007, 12:11 PM
You're welcome.
mattgoody
01-02-2007, 12:16 PM
ah nevermind my last question, i will just put a statement like if !isset($_SESSION) then it equals the $_POST
NightShift58
01-02-2007, 01:37 PM
I thinkIF (isset($_POST['FGM'])):
$_SESSION['FGM'] = $_POST['FGM']
ENDIF;would be better, as that is the problem - an empty $_POST...
chestertb
01-02-2007, 03:14 PM
This little snippet from PHP's online Session documentation might assist...
If you assign a session subscript/key to the same name as a variable, the session variable will be volatile and lost upon navigating.
For example, if passing a setting that you want in $_SESSION, don't do this:
<?
$setting = $_REQUEST['setting'];
if (!empty($setting))
$_SESSION['setting'] = $setting;
?>
Instead, rename $setting or $_SESSION['setting'].
I'd also move hot.css down to your html header so that all of the session data manipulation is complete before a header is sent to the browser.
Cheers
CTB
NightShift58
01-02-2007, 03:38 PM
Good info. Never noticed that one...