根据下拉选择显示表结果

时间:2017-03-01 19:56:35

标签: php jquery html sql-server

我有一个由我的数据库中的列填充的下拉列表。在选择时,我希望它显示任何表格结果,其中列等于下拉列表中选择的值。每当我选择一个值时,我都可以显示表头,但是没有显示其他数据。

我正在做echo $q并且它返回正确的值。所以它似乎正在获取所选值并正确地将其传递给test1.php,所以我猜我的test1.php脚本中某处出现了错误。

如何解决此问题,以便显示具有与所选内容相等的列Product Report Code的数据库行?

HTML / PHP:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","test1.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
<section id="rep_dropdown">
    <select onchange="showUser(this.value)" name="users">       
        <option value="">Product Report Code</option>
        <?php foreach($repcode->fetchAll() as $reportcode) { ?>
            <option value="<?php echo $reportcode['Product Report Code'];?>"><?php echo $reportcode['Product Report Code'];?></option>
        <?php } ?>
    </select>
</section>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

test1.php:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

<?php
$q = intval($_GET['q']);
echo $q;

  $host="xxxxxxxxx"; 
  $dbName="xxxxxxxxx"; 
  $dbUser="xxxxxxxxxxxxxx"; 
  $dbPass="xxxxxx";

  $dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);

$sql="SELECT * FROM vProducts WHERE [Product Report Code] = '".$q."'";
$result = sqlsrv_query($dbh,$sql);

?>



<table id="skuTable" cellspacing="5" class="sortable">
        <tr class="ui-widget-header">
            <th style="display: none">Product ID</th>
            <th class="skuRow">Major Category</th>
            <th class="skuRow">Minor Category</th>
            <th class="skuRow">Report Code</th>
            <th class="skuRow">SKU</th>
            <th class="skuRow">SKU Description</th>
            <th class="skuRow">SKU Status</th>
            <th class="skuRow">Create Date</th>
            <th class="skuRow">Group ID</th>
            <th class="sorttable_nosort">Edit</th>
        </tr>


        <?php while($row = sqlsrv_fetch_array($result)) { ?>

        <tr class="Row" data-code="<?php echo $row['Product Report Code']?>">
            <td style="display: none" class="prod_id" id="product_id-<?php echo intval ($row['Product_ID'])?>"><?php echo $row['Product_ID']?></td>
            <td class="major_cat" id="major_cat-<?php echo intval ($row['Major Category'])?>"><?php echo $row['Major Category']?></td>
            <td class="minor_cat" id="minor_cat-<?php echo intval ($row['Minor Category'])?>"><?php echo $row['Minor Category']?></td>
            <td class="rep_code" id="rep_code-<?php echo intval ($row['Product Report Code'])?>" align="center"><?php echo $row['Product Report Code']?></td>
            <td class="sku" id="sku-<?php echo intval ($row['SKU'])?>" align="center"><?php echo $row['SKU']?></td>
            <td class="sku_desc" id="sku_desc-<?php echo intval ($row['SKU Description'])?>"><?php echo $row['SKU Description']?></td>
            <td class="sku_status" id="sku_status-<?php echo intval ($row['SKU Status'])?>" align="center"><?php echo $row['SKU Status']?></td>
            <td class="create_date" id="create_date-<?php echo intval ($row['Date'])?>" align="center"><?php echo $row['Date']?></td>
            <td class="group_id" id="group_id-<?php echo intval ($row['Group_ID'])?>" align="center"><?php echo $row['Group_ID']?></td>
            <td><input type="button" class="edit" name="edit" value="Edit" onclick="enable_value(this)"></td>
        </tr>

    <?php } ?>

</table>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

我不确定sql server是否接受这样的表列名称Product Report Code

如果为true,那么您的连接存在问题,

$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);

$sql="SELECT * FROM vProducts WHERE [Product Report Code] = '".$q."'";
$result = sqlsrv_query($dbh,$sql);

您正在使用PDO扩展程序与sqlsrv扩展名完全不同。

您必须选择一种方式连接到您的数据库。

这是一个完整的PDO示例:

$dbh = new PDO( "sqlsrv:server=".$host."; Database=".$dbName, $dbUser, $dbPass);

$sql="SELECT * FROM vProducts WHERE [Product Report Code] = :placeholder";
$stm = $dbh->prepare($sql);
$result = $stm->execute(array("placeholder" => $q);

修改

您还需要更新此行:

<?php while($row = sqlsrv_fetch_array($result)) { ?>

到:

<?php while($row = $stm->fetch()) { ?>