2013-10-01 27 views
-4

當我在選擇onchange事件中選擇ALL時,while循環未執行。當我選擇全部時如何顯示所有記錄

dropdown.php 

     <script> 
     function showUser(str) 
     { 
     if (str=="") 
      { 
      document.getElementById("txtHint").innerHTML=""; 
      return; 
      } 
     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
      } 
     else 
      {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
      } 
      } 
     xmlhttp.open("GET","getuser.php?q="+str,true); 
     xmlhttp.send(); 
     } 
     </script> 

    <?php 
    $mysqli = new mysqli("localhost", "root", "", "app"); 
    $result = $mysqli->query("SELECT rfq FROM procurement GROUP BY rfq ORDER BY rfq"); 

    $option = ''; 
    while($row = $result->fetch_assoc()) 
    { 
     $option .= '<option value = "'.$row['rfq'].'">'.$row['rfq'].'</option>'; 
    } 
    ?> 

    <select name="users" onchange="showUser(this.value)"> 
      <option value="ALL" selected='ALL'>ALL</option> 
      <?php echo $option; ?> 
    </select> 

    <br> 
    <div id="txtHint"></div> 

getuser.php

<?php 
    $mysqli = new mysqli("localhost", "root", "", "app"); 
    $q=$_GET["q"]; 

    $result1 = $mysqli->query("SELECT *,SUM(unit_cost*quantity) AS total_amount FROM procurement WHERE rfq='".$q."' GROUP BY counter ORDER BY rfq"); 

    echo'<table id="tfhover" cellspacing="0" class="tablesorter"> 
      <thead> 
      <tr> 
       <th id="none" class="none" title="RFQ"></th> 
       <th title="RFQ">RFQ #</th> 
       <th title="Item Name">Item Name</th> 
       <th title="Item Description">Description</th> 
       <th title="Example : Pc, Pcs, Box and Etc.">Unit</th> 
       <th title="Item Price">Unit Cost</th> 
       <th title="Total Item Quantity">QTY</th> 
       <th title="Total Price">Total Amount</th> 
      </tr> 
      </thead>'; 
      echo'<tbody>'; 
    while($row = $result1->fetch_assoc()){ 
    echo'<tr> 
       <td align="center"><a href="comments.php?pn='.$row["rfq"].'"><img src="images/remarks.png" border="0" width="10" height="10" title="Remarks and Notes"></a></td> 
       <td>'.$row['rfq'].'</td> 
       <td>'.$row['item_name'].'</td> 
       <td>'.$row['item_description'].'</td> 
       <td>'.$row['unit'].'</td> 
       <td>'.number_format($row['unit_cost'], 2, '.', ',').'</td> 
       <td>'.$row['quantity'].'</td> 
       <td>'.number_format($row['total_amount'], 2, '.', ',').'</td> 
      </tr>'; 
      } 
     echo "</tbody></table>"; 


    echo $q; 
    if (!$mysqli) { 
     die('Connect Error: ' . mysqli_connect_error()); 
    } 
    mysqli_close($mysqli); 
     ?> 

enter image description here

我身體設置爲showuser(STR = 「ALL」),但就像在圖片時,我選擇所有的while循環ISN不執行。問題是什麼?

回答

1

getuser.php必須通過有條件地提供where子句來反映「ALL」的含義。

$q = $_GET["q"]; 
$where = ''; 
if ($q != 'ALL') { 
    $where = " WHERE rfq='$q' "; 
} 
$result1 = $mysqli->query(" 
    SELECT *,SUM(unit_cost*quantity) AS total_amount 
    FROM procurement 
    $where 
    GROUP BY counter ORDER BY rfq 
"); 

請注意,$_GET["q"]的價值沒有被清除掉,從而直接使用它在SQL查詢中可能導致SQL Injection

+0

太棒了!謝謝:)另外一件事。當鼠標懸停時,我的表格有一個腳本,排序和行的顏色。但這不起作用。我認爲我的腳本在上面影響其他腳本getuser.php –

+0

請張貼您的Javascript相關的問題,並讓大腦思考。順便說一句,如果我的解決方案解決了你的問題,你可以標記我的答案爲接受:) – Kita

+0

沒有標記@Kita haha​​hahaha – user2705620

相關問題