问题
Still banging my head, but I think an answer may help me see how things really work. I always sucked at languages, but I figure with your help I can brute force this.
So I am just going to post the whole code so as not to cause confusion.
<html>
<head>
<title>Change a player</title>
</head>
<BODY >
<?php
include "inc.php";
session_start();
print 'Select week for season: ';
print $this_season;
##the week drop down query#
$wquery=
"select week_num,week_name
from stats_week
where season=$this_season
order by week_num";
$wresult=mysql_query($wquery);
print'<form action="changeplayer_2.php" method="post">';
session_start();
print"<select name='Week_select'> <br>";
while ($wrow=mysql_fetch_array($wresult))
{
print '<option value="'.$wrow['week_num'].'">'.'week '.$wrow['week_num'].' '.$wrow['week_name'].'</option><br>\n';
}
print "</select><br><br>";#
print'<button type="submit" >Next</button>';
$varWeek=$_POST['Week_select'];
print"</form>";
$_SESSION['week'] = $varWeek;
?>
</body>
</html>
So, I don't know how to get 'Week_select' to be assigned to $_SESSION['week']. Above code was my last ditch effort. Secondly, once I assign something to the Session, how do I clear it out later so it doesn't keep popping up? Bonus question
print 'Select week for season: ';
print $this_season;
Is the only way I can make the both the statement and the variable to display. When I do
print 'Select week for season: '$this_season;
I don't get the variable displayed, also when I do
print 'Select week for season: $this_season';
I get Select week for season: $this_season displayed
回答1:
session_start() should be placed before output or any $_SESSION interaction. And only once.
<?php
session_start();
?>
<html>
<head>
<title>Change a player</title>
</head>
<BODY >
<?php
include "inc.php";
// etc...
?>
</body>
</html>
UPDv1:
if you need to remove something from session, then you can perform:
<?php unset($_SESSION['something']); ?>
UPDv2:
If you need tu put entire select into $_SESSION, you may do:
ob_start();
print "<select name='Week_select'>";
while ($wrow=mysql_fetch_array($wresult))
{ print '<option value="'.$wrow['week_num'].'">week '.$wrow['week_num'].' '.$wrow['week_name'].'</option>'.PHP_EOL; }
print "</select>";
$_SESSION['something'] = ob_get_clean();
// var_dump($_SESSION);
回答2:
When displaying (print/echo) a variable with a specific string, you must concatenate the two with " . " (dot)
print 'Select week for season: ' . $this_season;
回答3:
Session unset and destroy: here
Or you could just unregister the one variable: session_unregister($week)
And yes you should only have once session_start per page ideally before anything is output. And the correct way to print is to concatenate:
print "Select week for season: " . $this_season;
回答4:
Place your session_start() at the top of the html. Remove the second session_start() in the page.
Try printing/echoing your $_SESSION['week'] to check if it has a value.
print $_SESSION['week'];
回答5:
Your first question:
Part 1
To set a session the code should be inserted before the HTML tags. Read code below:
<?php
session_start();
$_SESSION['week'] = $this_season;
?>
<html>
<head>
//your code here
</head>
<body>
//your code here
</body>
</html>
Part 2
If you want to remove a session completely you can use the following code, which will destroy the session:
<?php session_destroy(); ?>
Or if you only want to clear a session you can use the following code:
<?php unset($_SESSION['week']); ?>
It will clear the session value.
Your second question:
To merge both print syntax together you need to use a dot "." . In some other language "+" is used. The process is called concatenation
Your new code should be:
print 'Select week for season: ' . $this_season;
This means that you want to print the string 'Select week for season: ' followed by the variable $this_season.
来源:https://stackoverflow.com/questions/15962443/need-more-detail-tying-in-session-start-to-a-select-statement