在php中进行会话检查后无法显示表

时间:2015-04-30 07:54:55

标签: php html mysql forms html-table

我添加了一些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>

1 个答案:

答案 0 :(得分:1)

您的表格显示取决于以下while condition

while($director=mysql_fetch_assoc($result)){

(我也相信你的错误是line 79

错误意味着您没有设置变量$ result。由于它没有定义,它也没有任何价值 - 所以基本上你用一个未定义的变量而不是查询来运行mysql_fetch_assoc函数。

现在,我注意到代码顶部已经定义了一个名为$result 的变量,如果您仔细研究一下 - 您可以在一个if condition,以及else选项 - 您不会。

结论 - 您的脚本会进入else块,因此$results未定义。请考虑调试代码,以便了解if condition返回false并转到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_。考虑使用PDOmysqli_