1

我正在尝试构建一个登录过程,通过使用 $_SESSION 变量,用户的登录凭据被存储并用于在屏幕上显示他们来自数据库的相关数据(即他们只会看到他们工作的学校数据)。

   <?php    
session_start();

if(!isset($_SESSION['Initials'], $_SESSION['Surname']))
{

 $host = "xxx";
 $username = "xxx";
 $password = "xxx";
 $database_name = "xxx";
 $table_name = "xxx";

 mysql_connect($host, $username, $password) OR die("Can't  
 connect");
 mysql_select_db($database_name) OR die("Can't connect to 
 Database");

 $query = "SELECT Class FROM $table_name WHERE Initials = '". 
 $_SESSION['Initials']."' AND staff LIKE '%".$_SESSION['Surname']."'";
 $result = mysql_query($query);
 $class = mysql_fetch_array($result);
 $count = mysql_num_rows($result);

 if($count === NULL)
 {
  echo "ERROR";
 }
  else
  {
  $_SESSION['Class'] = $result;
  echo "Class added to sessions";
  }
}
?>

通过为 if 语句添加正确的括号,我的查询无法识别会话变量的初始问题很容易解决。我在这里出现的下一个问题是,即使查询应该是成功的(当 $count 为 FALSE 或 NULL 时,我没有收到一条错误消息说“错误”)它没有将结果数组创建到新会话中,因为当我在新页面上打印会话数组时,它仍然只保留“姓名首字母”和“姓氏”会话。

为了使该数组(因为它必然会抛出多个结果)进入一个新会话,我需要对我的查询或查询后过程进行哪些更改?

非常感谢您对我最初的问题的回答!

4

2 回答 2

1
if(!isset($_SESSION['Initials'], $_SESSION['Surname'])) {
// code
}

你需要 { } 括号

if(!isset($_SESSION['姓名首字母'], $_SESSION['姓氏']))

$主机=“xxxxx”;$用户名 = "xxxxx"; $密码=“xxxxx”;

if(!isset($_SESSION['Initials'], $_SESSION['Surname'])) {

$host = "xxxxx";
}
$username = "xxxxx";
$password = "xxxxx";
于 2013-10-10T10:34:03.420 回答
0

我找到了答案 - 结果我没有将会话变量之一视为正确的数组,因此无法正确加载。我在下面添加了我的脚本,以便将来有类似问题的人可以将其用作参考点:

<?php

 session_start();


 // Server Details //
 $host = "---";
 $username = "---";
 $password = "---";
 $database_name = "---";
 $table_name = "---";


 // Connect Command //
 mysql_connect($host, $username, $password) OR die("Can't  
 connect");
 mysql_select_db($database_name) OR die("Can't connect to 
 Database");


 // Query to call up the unique school name //
 $query_school = mysql_query("SELECT DISTINCT School FROM $table_name 
 WHERE Initials = '".$_SESSION['---']."'
 AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
 $result_school = mysql_result($query_school, 0);


 // Query to call up the unique centre no //
 $query_centreno = mysql_query("SELECT DISTINCT CentreNo FROM 
 $table_name WHERE Initials = '".$_SESSION['---']."'
 AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
 $result_centreno = mysql_result($query_centreno, 0);


 // The newly created sessions for school info //
 $_SESSION['---'] = $result_school;
 $_SESSION['---'] = $result_centreno;


 // Query to call up the array of classes //
 $query_class = mysql_query("SELECT Class FROM $table_name WHERE 
 Initials = '".$_SESSION['---']."'
 AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
 $query_class__array = array();
 while($row = mysql_fetch_assoc($query_class))
 $query_class_array[] = $row;

 $_SESSION['---'] = $query_class_array;

?>
于 2013-10-11T10:34:11.650 回答