2015-04-07 41 views
1

我想讓講師選擇他們的名字和年份,以便顯示該年份的統計表格。我有不同的講師。我爲每位講師創建了不同的表格。當我選擇第一位講師時,系統顯示正確的表格,但是當我選擇第二位講師時,系統會顯示第一位講師的表格。另外,當我選擇lec12006時,它顯示lec1表格,但顯示2005年的分數。無法在php中顯示正確的表格

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> 

回答

1

這不是編寫代碼的好方法。而不是你做什麼,只是把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> 
     ... 
+0

非常感謝你! –

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

我能做些什麼來返回2006年的結果?有什麼辦法嗎? –

+0

好的謝謝它運行它dipalyed爲lec1和2006年右桌...再次非常感謝你:) –

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