无法在PHP中显示正确的表

时间:2015-04-07 09:02:51

标签: php sql html-table

我希望讲师选择他们的名字和一年,以便显示该年的统计表。我有不同的讲师。我为每位讲师创建了不同的表格。当我选择第一个讲师时,系统显示正确的表格,但是当我选择第二个讲师系统时,它会显示第一个讲师的表格。另外,当我选择lec12006时,它会显示lec1表,但会显示2005年的分数。

lec2 db table

lec1 db table

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>

3 个答案:

答案 0 :(得分:1)

你在WHERE子句中有2005年的硬编码。它将始终返回2005年的结果。

另外,

将切换句更改为:

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"';
}

答案 1 :(得分:1)

您的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";

答案 2 :(得分:1)

这不是编写代码的好方法。而不是你做了什么,只需加上讲话文件:

<?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>
       ...