【问题标题】:Cannot display the proper table in PHP无法在 PHP 中显示正确的表格
【发布时间】:2015-04-07 09:02:50
【问题描述】:

我希望讲师选择他们的姓名和年份,以便显示该年份的统计表。我有不同的讲师。我为每位讲师创建了不同的表格。当我选择第一个讲师时,系统会显示正确的表格,但是当我选择第二个讲师时,系统会显示第一个讲师的表格。另外,当我选择lec12006 时,它显示的是 lec1 表,但显示的是 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>

讲师.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 sql html-table


    【解决方案1】:

    这不是编写代码的好方法。而不是你所做的,只需放入讲师.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>
           ...
    

    【讨论】:

      【解决方案2】:

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

      另外,

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

      【讨论】:

      • 我能做些什么来返回 2006 年的结果?有什么办法吗?
      • 好的,谢谢它为 lec1 和 2006 年正确的表运行它...再次非常感谢你:)
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多