2013-04-29 44 views
0

排序的JavaScript我尝试整理一些数据是从数据库中检索表使用select选项菜单

首先,用户将选择他们想要从选择选项菜单显示后

哪个国家的数据时,接下来的页面将显示在表中的特定数据进行排序递增/递减功能

这是我的代码

第一页的test.html

<form action="showDB1.php" method="post"> 
<table border="0"> 
<tr> 
    <th colspan="3">test</th> 
</tr> 
<tr> 
    <td>Select Foreign Agent Country</td> 
    <td></td> 
    <td> 
    <select name="country"> 
    <option value="US">United States</option> 
    <option value="NZ">New Zealand</option> 
    <option value="JP">Japan</option> 
    </select> 
    </td> 
    </tr> 
    <td colspan="3"> 
    <input type="submit" name="formSubmit" value-"Submit"> 
    </td> 
</table> 
</form> 

这是我的代码,以显示数据之后,用户选择他们想显示showDB1.php哪个国家的数据

<?php 
//connect to server 
$connect = mysql_connect("localhost", "root", ""); 

//connect to database 
//select the database 
mysql_select_db("fak_databases"); 
//submit button 
if($_POST['formSubmit'] == "Submit") 
{ 
    $country = $_POST['country']; 
} 

//query the database 
if($country == TRUE) { 
    $order = ""; 
    $sort = "asc"; 
    $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample"; 
    if(isset($_GET['orderby'])){ 
     $order = $_GET['orderby']; 
     $sort = $_GET['sort']; 

     //limiting the possible values of order/sort variables 
     if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1')$order = "applicant1_addr1"; 
      if($sort != 'asc' && $sort != 'desc')$sort = "asc"; 
       $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

       //here we reverse the sort variable 
       if($sort == "asc"){ 
        $sort = "desc"; 
       } 
      else{ 
       $sort = "asc"; 
      } 
     } 
    // query to get all US records 
} 
    $result = mysql_query($sql); 
    $num_rows = mysql_num_rows($result); 
    $row_counter = 0; 

    $icon = ""; 
    echo "<table border=\"1\" cellspacing=\"0\">\n"; 
    echo "<tr>\n"; 

    // first column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "wipo_applicant1_city"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 

    //print the result 
    echo "<a href='index.php?orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; 
    echo "</th>\n"; 


    // second column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "applicant1_addr1"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 
    echo "<a href='index.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; 
    echo "</th>\n"; 
    echo "</tr>"; 

//fetch the result 

while($row = mysql_fetch_array($result)) 
{ 
    if($row_counter % 2){ 
      $row_color="bgcolor='#FFFFFF'"; 
     }else{ 
      $row_color="bgcolor='#F3F6F8'"; 
     } 
    echo "<tr class=\"TrColor\" ".$row_color.">"; 
    echo "<td>" . $row['wipo_applicant1_city'] . "</td>\n"; 
    echo "<td>" . $row['applicant1_addr1'] . "</td>\n"; 
    echo "</tr>"; 
    $row_counter++; 
} 

Print "</table>"; 

我的问题是,当用户选择的任何一个国家所有的数据显示选择选项。

预期结果应该只是需要显示的特定数据

任何人都可以修复这个问题吗?

感谢

+2

不错的SQL注入孔...(你忘了逃跑$国家和$ sort ...) – 2013-04-29 16:12:44

+0

我不明白你的意思@MarcB ....我只是在这方面的新手.... anw,谢谢你的回应 – user2331284 2013-04-29 16:59:21

回答

0

从看你的代码,该问题可能是你有一个破折号等号,而不是在你的输入按钮

<input type="submit" name="formSubmit" value-"Submit"> 

value="Submit" 

我会看看下面的语句,然后

if(isset($_GET['orderby'])){ 

由于在您首次提交表单时,您发送的是后置语句,因此您的sql语句不会被调用,因为它被包含在该if语句中。

此举声明

$order = ""; 
$sort = "asc"; 
$sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample"; 

,当然添加此为你的国家的选择之外以下露面

WHERE applicant1_country='$country' 
+0

感谢您的回应...但结果是还是一样 – user2331284 2013-04-29 16:58:25

+0

没问题,我编辑了我的答案,希望有你正在寻找的修复程序,使您的代码工作 – 15km 2013-04-29 17:43:10

+0

是它就像我在代码中编辑的那样...如果($ country == TRUE){.... if所以,我得到了2个错误,这是mysql_num_rows()和mysql_fetch_array()...都期望1个参数...无论如何,谢谢你帮助我,直到现在:) – user2331284 2013-04-30 12:53:02