将会话检查放入 php 后无法显示 table
Cannot display the table after putting session checks in php
我添加了一些 if
条件和 php
会话。现在它无法显示我的程序表。我得到一个错误:Undefined variable $result on line 79
。在其他操作文件中,表格工作正常,它们会显示出来。
ac_directorProgramsYearsForm.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="form">
<form name="myform" action="ac_directorProgramsYears.php" method="POST">
<b>Programs:<b/>
<select name="program">
<option value="Choose">Please select..</option>
<?php
$sql = mysql_query("SELECT prog_name FROM program");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='".$row['prog_name']."'>".$row['prog_name'] ."</option>";
}
?>
</select><br/><br/>
<br/>
<input type="submit" value="submit" name="Submit">
<input type="reset" name="reset" value="Clear">
</form>
</div>
</body>
</html>
ac_directorProgramsYears.php
<?php
include 'connect.php';
$programs = array(
'bsc_computer_science',
'bsc_psychology',
'ba_finance',
'ba_marketing',
'ba_management'
);
if(isset($_POST['submit'])){
$program = mysql_real_escape_string($_POST['program']);
session_start();
if(!isset($_SESSION['username'])){
header("location:../../statistics/main.htm");
}
$username=$_SESSION['username'];
if(isset($_POST['program'])){
$_SESSION['program'] = $_POST['program'];
if (in_array($program, $programs)){
$sql = "SELECT year,a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $program";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="container">
<table id="table" width="900" border="1" cellspacing="1">
<tbody>
<tr>
<td>Year</td>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>L3 </td>
<td>L4 </td>
<td>L5 </td>
<td>L6 </td>
<td>L7 </td>
<td>LAVG </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tr>
</tbody>
<?php
while($director=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$director['year']."</td>";
echo "<td>".$director['a1']."</td>";
echo "<td>".$director['a2']."</td>";
echo "<td>".$director['a3']."</td>";
echo "<td>".$director['l1']."</td>";
echo "<td>".$director['l2']."</td>";
echo "<td>".$director['l3']."</td>";
echo "<td>".$director['l4']."</td>";
echo "<td>".$director['l5']."</td>";
echo "<td>".$director['l6']."</td>";
echo "<td>".$director['l7']."</td>";
echo "<td>".$director['lavg']."</td>";
echo "<td>".$director['r1']."</td>";
echo "<td>".$director['r2']."</td>";
echo "<td>".$director['u1']."</td>";
echo "<td>".$director['u2']."</td>";
echo "<td>".$director['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
您的 table 的显示取决于以下 while condition
:
while($director=mysql_fetch_assoc($result)){
(我也认为这是 line 79
来自你的错误)
该错误表示您没有设置变量$result。因为它没有定义,所以它也没有任何价值 - 所以基本上你是 运行 带有未定义变量而不是查询的 mysql_fetch_assoc
函数。
现在,我注意到您在代码的顶部定义了一个名为 $result
but 的变量,如果您仔细观察 - 您定义if condition
和 else
选项中的那个变量 - 你没有。
结论 - 您的脚本进入 else
块,因此 $results
未定义。考虑调试您的代码以了解为什么 if condition
returns 错误并转到 else
块。
if (in_array($program, $programs)){//This condition return false so...
$sql = "...."
$result = mysql_query($sql);
} else { //Your script runs this part of code
echo "No data found";
//$result is undefined in this case.
}
请注意 您使用的 mysql_
已被弃用。考虑使用 PDO
或 mysqli_
。
我添加了一些 if
条件和 php
会话。现在它无法显示我的程序表。我得到一个错误:Undefined variable $result on line 79
。在其他操作文件中,表格工作正常,它们会显示出来。
ac_directorProgramsYearsForm.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="form">
<form name="myform" action="ac_directorProgramsYears.php" method="POST">
<b>Programs:<b/>
<select name="program">
<option value="Choose">Please select..</option>
<?php
$sql = mysql_query("SELECT prog_name FROM program");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='".$row['prog_name']."'>".$row['prog_name'] ."</option>";
}
?>
</select><br/><br/>
<br/>
<input type="submit" value="submit" name="Submit">
<input type="reset" name="reset" value="Clear">
</form>
</div>
</body>
</html>
ac_directorProgramsYears.php
<?php
include 'connect.php';
$programs = array(
'bsc_computer_science',
'bsc_psychology',
'ba_finance',
'ba_marketing',
'ba_management'
);
if(isset($_POST['submit'])){
$program = mysql_real_escape_string($_POST['program']);
session_start();
if(!isset($_SESSION['username'])){
header("location:../../statistics/main.htm");
}
$username=$_SESSION['username'];
if(isset($_POST['program'])){
$_SESSION['program'] = $_POST['program'];
if (in_array($program, $programs)){
$sql = "SELECT year,a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $program";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="container">
<table id="table" width="900" border="1" cellspacing="1">
<tbody>
<tr>
<td>Year</td>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>L3 </td>
<td>L4 </td>
<td>L5 </td>
<td>L6 </td>
<td>L7 </td>
<td>LAVG </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tr>
</tbody>
<?php
while($director=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$director['year']."</td>";
echo "<td>".$director['a1']."</td>";
echo "<td>".$director['a2']."</td>";
echo "<td>".$director['a3']."</td>";
echo "<td>".$director['l1']."</td>";
echo "<td>".$director['l2']."</td>";
echo "<td>".$director['l3']."</td>";
echo "<td>".$director['l4']."</td>";
echo "<td>".$director['l5']."</td>";
echo "<td>".$director['l6']."</td>";
echo "<td>".$director['l7']."</td>";
echo "<td>".$director['lavg']."</td>";
echo "<td>".$director['r1']."</td>";
echo "<td>".$director['r2']."</td>";
echo "<td>".$director['u1']."</td>";
echo "<td>".$director['u2']."</td>";
echo "<td>".$director['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
您的 table 的显示取决于以下 while condition
:
while($director=mysql_fetch_assoc($result)){
(我也认为这是 line 79
来自你的错误)
该错误表示您没有设置变量$result。因为它没有定义,所以它也没有任何价值 - 所以基本上你是 运行 带有未定义变量而不是查询的 mysql_fetch_assoc
函数。
现在,我注意到您在代码的顶部定义了一个名为 $result
but 的变量,如果您仔细观察 - 您定义if condition
和 else
选项中的那个变量 - 你没有。
结论 - 您的脚本进入 else
块,因此 $results
未定义。考虑调试您的代码以了解为什么 if condition
returns 错误并转到 else
块。
if (in_array($program, $programs)){//This condition return false so...
$sql = "...."
$result = mysql_query($sql);
} else { //Your script runs this part of code
echo "No data found";
//$result is undefined in this case.
}
请注意 您使用的 mysql_
已被弃用。考虑使用 PDO
或 mysqli_
。