不能在另一个页面中使用会话变量的值

时间:2016-07-26 05:35:18

标签: php mysql sql

我在session AKA index page中创建了login page,当我尝试在另一个页面的select查询中使用该值时,任何人都无法帮助我{ {3}}

enter image description here

    <?php 
session_start();

$connection = mysql_connect('localhost','root','root') or die ("Couldn't connect to server.");  
$db = mysql_select_db('test', $connection) or die ("Couldn't select database.");  


$data ="select user_id,first_name,last_name,father_name,address,pincode,dob,phone from acc where user_id='" .$_SESSION['userSession']. "'"; 
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
  $data2 = mysql_fetch_array($query); 

?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
      <title></title> 
 </head> 

<body> 

<!-- form to display record from database --> 
<center>
<form name="form" method="POST" action="abcd2.php"> 
  first Name: <input type="text" name="firstname" value="<?php echo  $data2 ['first_name']?>"/> <br> 
 last Name : <input type="text" name="lastname" value="<?php echo  $data2 ['last_name']?>"/> <br>
 father Name: <input type="text" name="fathername" value="<?php echo  $data2 ['father_name']?>"/> <br> 
 address: <input type="text" name="address" value="<?php echo  $data2 ['address']?>"/> <br> 
 pincode: <input type="text" name="pincode" value="<?php echo  $data2 ['pincode']?>"/> <br> 
  DOB: <input type="text" name="dob" value="<?php echo  $data2 ['dob']?>"/><br>
  phone: <input type="text" name="phone" value="<?php echo  $data2 ['phone']?>"/> <br> 
      <input type="submit"  value="submit"> 
</form> 
</center>
</body> 

</html>

2 个答案:

答案 0 :(得分:1)

你忘记开始会议了!

在为session_start()超级全局和其他页面$_SESSION分配任何内容之前使用session_start,然后再检索任何内容,以便您可以恢复会话

希望你明白了

答案 1 :(得分:1)

你必须使用

session_start();

以获取会话变量的值

http://php.net/manual/en/function.session-start.php