无法在PHP中显示正确的表 [英] Cannot display the proper table in PHP

查看:204
本文介绍了无法在PHP中显示正确的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want the lecturer to select their name and a year so to display a statistical table for that year. I have different lecturers. I have created different table for each lecturer. When I select the first lecturer the system displays the right table, but when I select the second lecturer the system it displays the table from the first lecturer. Plus when I select the lec1 and 2006, it displays the lec1 table but the scores of 2005 year.

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

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>


推荐答案

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

It's not a good way to write your code. Instead of what you did, just put on the lecturer.php file:

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

这篇关于无法在PHP中显示正确的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆