【问题标题】:How to display ALL records when I select ALL选择 ALL 时如何显示所有记录
【发布时间】:2013-10-07 00:38:49
【问题描述】:

当我在 select 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);
        ?>

我将 body 设置为 showuser(str="ALL"),但就像在图片中我选择 ALL 时那样 while 循环没有执行。有什么问题?

【问题讨论】:

    标签: javascript php jquery mysql mysqli


    【解决方案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

    【讨论】:

    • 太棒了!谢谢:) 还有一件事。当鼠标悬停时,我的表格、行的排序和颜色有一个脚本。但这行不通。我认为我上面的脚本会影响 getuser.php 中的其他脚本
    • 请发表您的 Javascript 相关问题,让大脑思考。顺便说一句,如果我的解决方案解决了您的问题,您可以将我的答案标记为已接受:)
    • @AsshO.Le 是的,如你所见:)
    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2016-01-25
    • 1970-01-01
    • 2019-05-23
    • 2017-12-02
    • 1970-01-01
    • 2023-03-21
    • 2013-04-17
    相关资源
    最近更新 更多