我有两个php文件。第一部分是:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
$city = stripslashes($city);
$movie = stripslashes($movie);
$date = stripslashes($date);
$_SESSION['city'] = $city;
$_SESSION['movie'] = $movie;
$_SESSION['date'] = $date;
?>
<?php
//$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("movie_booking", $con);
?>
<title>Book Ur Show</title>
<style type="text/css">
a:link {
color:#ffffff;
text-decoration: underline;
}
a:visited {
color: #ffffff;
text-decoration: underline;
}
html, body {height:100%; margin:0; padding:0;}
#page-background {position:fixed; top:0; left:0; width:100%; height:100%;}
#content {position:relative; z-index:1; padding:10px;}
</style>
</head>
<body>
<div id="page-background"><img src="images/main%20baclground.jpg" width="100%" height="100%" alt="Smile"></div>
<center>
<div class="container" style="width:800px" id="content">
<div class="header"><img src="images/logo.png" width="177" height="61" longdesc="main.php" /> <!-- end .header --></div>
<center>
<div class="content" style="background-image:url(); height:427px; color: #FFF;">
<p align="right"><?php $username = $_SESSION['myusername'];
$sql= "select * from users_tbl where username='$username' and userlevel='9'";
$result = mysql_query($sql);
if($row = mysql_fetch_array($result))
{
echo "[<a href=\"admin.php\">Admin Center</a>]";
}
?> [<a href="first.php">Main Page</a>] [<a href="logout.php">Logout</a>]</p><p align="left"><?php
$username = $_SESSION['myusername'];
echo "Welcome $username";
?></p>
<form name="form1" action="book.php" method="post">
<table width="200" border="0">
<tr>
<td>City</td>
<td><input name="city" type="text" id="city" readonly="true" style="background-color:#000; color:#FFF" value="<? $sql="Select * from city where city_id='$city'";$sqlresult=mysql_query($sql);$row = mysql_fetch_array($sqlresult);$cityname=$row['city_name'];echo $cityname;?>" /></td>
</tr>
<tr>
<td>Movie</td>
<td><input name="movie" type="text" id="movie" readonly="true" style="background-color:#000; color:#FFF" value="<? $sql="Select * from movie where movie_id='$movie'" ;$sqlresult=mysql_query($sql);$row = mysql_fetch_array($sqlresult);$moviename=$row['movie_name'];echo $moviename;?>" /> </td>
</tr>
<tr>
<td>Date</td>
<?php echo var_dump($_SESSION['city']); ?>
<td><input name="date" type="text" id="date" readonly="true" style="background-color:#000; color:#FFF" value="<? $sql="Select * from movie where date='$date' and movie_id='$movie' and city_id='$city'";$sqlresult=mysql_query($sql);$row = mysql_fetch_array($sqlresult);$date2=$row['date'];echo $date2;?>" /></td>
</tr>
</table>
<?php
echo "<br><br>";
//Art of MySQL
$sql = "Select movie_name,theatre_id, date, showtiming from movie where 1";
//Check if movie is not null
if( strlen($movie)>0 )
{
$sql.= " and movie_id = $movie ";
}
if( strlen($city)>0 )
{
$sql.= " and city_id = '$city' ";
}
if( strlen($date)>0 )
{
$sql.= " and date='$date' ";
}
$result = mysql_query($sql);
echo "<table>";
echo "<tr>
<td width=\"100px\">Movie</td>
<td width=\"100px\"> Theatre</td>
<td width=\"100px\"> Date </td>
<td width=\"100px\">Show Timing</td>
<td width=\"100px\">Book Ticket</td></b>
</tr>";
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($result))
{
echo "<form name=\"form1\" action=\"book.php\" method=\"post\">";
$sql2 = "Select theatre_name from theatre where theatre_id=".$row['theatre_id']."";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
$mname = $row['movie_name'];
$tname = $row2['theatre_name'];
$stime = $row['showtiming'];
$date = $row['date'];
echo "<tr>
<td><input name=\"mname\" type=\"text\" id=\"mname\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$mname'/></td>
<td><input name=\"tname\" type=\"text\" id=\"tname\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$tname'/></td>
<td><input name=\"date\" type=\"text\" id=\"date\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$date'/></td>
<td><input name=\"stime\" type=\"text\" id=\"stime\" readonly=\"true\" style=\"background-color:#000; color:#FFF\" value='$stime'/></td>
<td align=\"center\"><input name=\"book\" type=\"submit\" value=\"Book\" /></td>
</tr>";
echo "</form>";
}
echo "</table>";
?>
</form>
</div>
</center>
</body>
</html>
我已检查$_SESSION['city']
已正确设置,当用户稍后提交表单时,它将调用另一个php文件:
<?php
session_start();
$city = $_SESSION['city'];
echo "hey".$_SESSION['city'];
if(isset($_SESSION['city'])){
echo "is set";
}
?>
输出
heyis set
知道为什么我可以访问$ _session中的数据。欣赏任何想法。
修改
亚历克斯要求
我添加到这两个文件中。在最后一行末尾的第一个文件中我添加了
<?php echo var_dump($_SESSION); ?>
我得到了
array(8) { ["myusername"]=> string(6) "bkg988" ["password"]=> string(32) "827ccb0eea8a706c4c34a16891f84e7b" ["city"]=> string(1) "1" ["movie"]=> string(0) "" ["date"]=> string(0) "" ["data"]=> string(27) "you have an active session!" ["stime"]=> string(0) "" ["tname"]=> string(0) "" }
但是在第二个文件中我得到了
array(8) { ["myusername"]=> string(6) "bkg988" ["password"]=> string(32) "827ccb0eea8a706c4c34a16891f84e7b" ["city"]=> string(0) "" ["movie"]=> string(0) "" ["date"]=> string(0) "" ["data"]=> string(27) "you have an active session!" ["stime"]=> string(0) "" ["tname"]=> string(0) "" }
答案 0 :(得分:1)
尝试生成以下内容以查看它们是否有变量
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
,或使用$_REQUEST
代替$_POST
答案 1 :(得分:1)
问题是(正如您对我们进一步调试所证明的那样)第一个文件顶部的这段代码
<?
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
$city = stripslashes($city);
$movie = stripslashes($movie);
$date = stripslashes($date);
$_SESSION['city'] = $city;
$_SESSION['movie'] = $movie;
$_SESSION['date'] = $date;
?>
当您将表单发布到该页面时,所发生的一切都按预期工作。这些值已分配,当您检查底部的$_SESSION
变量时,它会很好。
如果您再次使用$_POST
数据浏览到该页面,因为$_POST
为空,您将分配$_SESSION
个变量,即不存在的变量的值。< / p>
所以解决方案分为两部分。
首先报错 - 启用错误报告。在开发时,你应该总是有错误报告(在你尝试使用不存在的$ _POST变量的情况下,这会发出警告)
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
其次,在使用之前检查变量是否存在 - 在使用之前检查$_POST
变量(包括$ _POST本身)是否已设置。
if(isset($_POST))
{
if(isset($_POST['city']))
{
$_SESSION['city'] = stripslashes($_POST['city']);
}
if(isset($_POST['movie']))
{
$_SESSION['movie'] = stripslashes($_POST['movie']);
}
if(isset($_POST['date']))
{
$_SESSION['date'] = stripslashes($_POST['date']);
}
}