Reputation: 4590
I have two php files. Part of first one is:
<?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>
And I have checked that $_SESSION['city']
is been set correctly and when user submit a form later it will call another php file which is:
<?php
session_start();
$city = $_SESSION['city'];
echo "hey".$_SESSION['city'];
if(isset($_SESSION['city'])){
echo "is set";
}
?>
And output is
heyis set
Any idea why I can access the data inside $_session. Appreciate any idea.
EDIT
As Alex asked for
I added to both file. In first file at the end last line I added
<?php echo var_dump($_SESSION); ?>
And I get
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) "" }
But in second file I get
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) "" }
Upvotes: 0
Views: 139
Reputation: 1984
The problem is (as demonstrated by your further debugging with us) this bit of code at the top of your first file
<?
$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;
?>
What is happening, when you POST your form to that page all works as expected. The values are assigned and when you check the $_SESSION
variable at the bottom its fine.
If you then browse to that page again with $_POST
data, because $_POST
is empty, you are assigning the $_SESSION
variables, values of a variable that doesn't exist.
So the solution is in 2 parts.
Firstly error reporting - Turn error reporting on. When developing you should always have error reporting on (In your case of trying to use $_POST variables that didn't exist, this would throw a warning)
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
Secondly, check variables exist before using them - Check your $_POST
variables (including $_POST itself) are set before you use them.
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']);
}
}
Upvotes: 1
Reputation: 390
Try to eco the following in order to see if they got variables
$city= $_POST['city'];
$movie= $_POST['movie'];
$date = $_POST['date'];
, or use $_REQUEST
instead of $_POST
Upvotes: 1