无法在 PHP 中显示正确的 table

Cannot display the proper table in PHP

我希望讲师 select 他们的名字和年份,以便显示那一年的统计数据 table。我有不同的讲师。我为每个讲师创建了不同的table。当我select第一个讲师时,系统显示正确的table,但是当我select第二个讲师时,系统显示第一个讲师的table。加上当我 select lec12006 时,它显示 lec1 table 但 2005 年的分数。

form.php

<form name="myform" action="lecturer.php" method="POST" >
   <b>Lecturers:<b/>
   <select name="lecturer">  
   <option value="Choose">Please select..</option>
   <option value="lec1">Lec 1</option> 
   <option value="lec2">Lec 2</option></select><br/><br/>

   <b>Year:<b/>
   <select name="year"> 
   <option value="Choose">Please select..</option>
   <option value="2005">2005</option> 
   <option value="2006">2006</option>
   <option value="2007">2007</option>
   <option value="2008">2008</option>
   <option value="2009">2009</option>
   <option value="2010">2010</option></select><br/><br/>


  <br/>
   <input type="submit" name="submit" value="Submit">
   <input type="reset" name="reset" value="Clear">

</form>

lecturer.php

     <?php

     switch($_POST['lecturer']&& $_POST['year']){

        case 'lec1' && '2005':
        include 'href="/../../statistics/lec12005.php"';
        break;

        case 'lec1' && '2006':
        include 'href="/../../statistics/lec12006.php"';
        break;

        case'lec2' && '2006':
        include 'href="/../../statistics/lec22006"';
    }



    ?>

lec1.php

    <?php
     include 'connect.php';

   $sql="SELECT unit_name,a1,a2,a3,l1,l2,r1,r2,u1,u2,u3 FROM lec1 WHERE year=2005";

   $result=mysql_query($sql);
?>

   <html>
   <body>
    <table width="900" border="1" cellspacing="1">
     <tr>
         <td>Unit Name</td>
         <td>A1 </td>
         <td>A2 </td>
         <td>A3 </td>
         <td>L1 </td>
         <td>L2 </td>
         <td>R1 </td>
         <td>R2 </td>
         <td>U1 </td>
         <td>U2 </td>
        <td>U3 </td>


    </tr>

    <?php
      while($unit=mysql_fetch_assoc($result)){
        echo "<tr>";
        echo "<td>".$unit['unit_name']."</td>";
        echo "<td>".$unit['a1']."</td>";
        echo "<td>".$unit['a2']."</td>";
        echo "<td>".$unit['a3']."</td>";
        echo "<td>".$unit['l1']."</td>";
        echo "<td>".$unit['l2']."</td>";
        echo "<td>".$unit['r1']."</td>";
        echo "<td>".$unit['r2']."</td>";
        echo "<td>".$unit['u1']."</td>";
        echo "<td>".$unit['u2']."</td>";
        echo "<td>".$unit['u3']."</td>";
        echo "</tr>";    
       }
    ?>
   </table>
   </body>
   </html>

您在 WHERE 子句中硬编码了 2005 年。 2005 年的结果总是 return。

另外,

将switch语句改为:

switch($_POST['lecturer'] . $_POST['year']){

    case 'lec1' . '2005':
    include 'href="/../../statistics/lec12005.php"';
    break;

    case 'lec1' . '2006':
    include 'href="/../../statistics/lec12006.php"';
    break;

    case'lec2' . '2006':
    include 'href="/../../statistics/lec22006"';
}

你的switchinclude逻辑都是完全错误的。

您应该验证您的 $_POST 变量(非常重要!)并替换:

 switch($_POST['lecturer']&& $_POST['year']){

    case 'lec1' && '2005':
    include 'href="/../../statistics/lec12005.php"';
    break;

    case 'lec1' && '2006':
    include 'href="/../../statistics/lec12006.php"';
    break;

    case'lec2' && '2006':
    include 'href="/../../statistics/lec22006"';
}

与:

include "path/to/statistics/"  . $_POST['lecturer'] . $_POST['year'] . ".php";

这不是编写代码的好方法。而不是你所做的,只需放入 lecturer.php 文件:

<?php
include 'connect.php';

$year = $_POST['year'];
$lecturer = $_POST['lecturer']; // Don't forget to handle the SQL Injections ...
$years     = array(
    2005,
    2006,
    2007
);
$lecturers = array(
    'lec1',
    'lec2'
);

if (in_array($lecturer, $lecturers) && in_array($year, $years)) {

    $sql = "SELECT unit_name,a1,a2,a3,l1,l2,r1,r2,u1,u2,u3 FROM $lecturer WHERE year=$year";

    $result = mysql_query($sql);
}

else {
    // Handle the error
}
?>

       <html>
       <body>
       ...