Ajax数据库选择值返回"0"而不是字符串 [英] Ajax Database select value returning '0' instead of string

查看:48
本文介绍了Ajax数据库选择值返回"0"而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二手车网站,该网站使用以下sql在页面上列出了所有当前库存:"SELECT * FROM库存量按时间戳ASC".

I have a website for a used car lot that lists all their current inventory on a page by using the following sql: "SELECT * FROM inventory ORDER BY timestamp ASC".

我正在尝试使用W3Schools( https://www.w3schools.com/php Ajax数据库操作的/php_ajax_database.asp )方法可在下拉菜单的选项更改后过滤我的结果.

I am trying to use the W3Schools (https://www.w3schools.com/php/php_ajax_database.asp) method of Ajax database manipulation to filter my results when an option of a dropdown menu has been changed.

javascript和php都能正常运行,但是在新sql语句中发送并使用的值显示为"0",而不是应发送的值.

The javascript and php all runs correctly but the value that is sent and is used in the new sql statement is showing as '0' instead of the value that should be sent.

此下拉菜单的值从同一张表中提取,以动态生成菜单,并且每个菜单项的值应为"Ford","Audi","Dodge"等.在由javascript发送之前,该方法都可以正常工作.

The values of this dropdown are pulled from the same table to dynamically generate the menu and the values for each should be 'Ford', 'Audi', 'Dodge', etc. Which works fine until it's sent by the javascript.

<select name="filtermake" class="css-dropdowns" tabindex="1" onchange="showMake(this.value)">
    <option value="">All Makes</option>
    <?php
        $sql = "SELECT DISTINCT make FROM inventory ORDER BY make ASC";
        $result = mysqli_query($conn, $sql);
        while ($row = $result->fetch_assoc()) {
            echo '<option value="'.$row['make'].'">'.$row['make'].'</option>';
        }
    ?>
</select>

用于动态替换的JS是:

The JS for the dynamic replacement is:

<script>
function showMake(str) {
    if (str == "") {
        document.getElementById("inventory").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("inventory").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","includes/inc.inventorymake.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

该脚本运行的PHP代码为:

The PHP code that is run by the script is:

<?php
    include_once ('inc.dbh.php');
    $q = intval($_GET['q']);
    $inventorysql = "SELECT * FROM inventory WHERE make = '".$q."'";
    $result = mysqli_query($conn, $inventorysql);
        while ($row = $result->fetch_assoc()) {
            echo '<div id="inventory" class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"><a class="inventory" href="inventory-listing.php?vin=' . $row['vin'] . '"><div class="title">'.$row['year'].' '.$row['make'].' '.$row['model'].'</div><img src="images/inventory/'.$row['vin'].'-main.jpg" class="preview" alt="preview"><table class="options-primary"><tr><td class="option primary">Body Style:</td><td class="spec">'.$row['body'].'</td></tr><tr><td class="option primary">Drivetrain:</td><td class="spec">'.$row['drivetrain'].'</td></tr><tr><td class="option primary">Transmission:</td><td class="spec">'.$row['transmission'].'</td></tr><tr><td class="option primary">Mileage:</td><td class="spec">'.ltrim ($row['mileage'], '0').'</td></tr></table><table class="options-secondary"><tr><td class="option secondary">Exterior Color:</td><td class="spec">'.$row['exteriorcolor'].'</td></tr><tr><td class="option secondary">Interior Color:</td><td class="spec">'.$row['interiorcolor'].'</td></tr><tr><td class="option secondary">VIN Number:</td><td class="spec">'.$row['vin'].'</td></tr></table><div class="price"><b>Price:</b><br><div class="figure">$'.ltrim ($row['price'], '0').'<br></div><div class="tax">Plus Tax, Tag, and Title</div></div><div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> View Details </div><div class="clearfix"></div></a></div>';     
        }
?>

最后,被javascript替换的div是:

And finally the div that is replaced by the javascript is:

<div id="inventory">
<?php
    $inventorysql = "SELECT * FROM inventory ORDER BY timestamp ASC";
    $result = mysqli_query($conn, $inventorysql);
        while ($row = $result->fetch_assoc()) {
            echo '<div id="inventory" class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"><a class="inventory" href="inventory-listing.php?vin=' . $row['vin'] . '"><div class="title">'.$row['year'].' '.$row['make'].' '.$row['model'].'</div><img src="images/inventory/'.$row['vin'].'-main.jpg" class="preview" alt="preview"><table class="options-primary"><tr><td class="option primary">Body Style:</td><td class="spec">'.$row['body'].'</td></tr><tr><td class="option primary">Drivetrain:</td><td class="spec">'.$row['drivetrain'].'</td></tr><tr><td class="option primary">Transmission:</td><td class="spec">'.$row['transmission'].'</td></tr><tr><td class="option primary">Mileage:</td><td class="spec">'.ltrim ($row['mileage'], '0').'</td></tr></table><table class="options-secondary"><tr><td class="option secondary">Exterior Color:</td><td class="spec">'.$row['exteriorcolor'].'</td></tr><tr><td class="option secondary">Interior Color:</td><td class="spec">'.$row['interiorcolor'].'</td></tr><tr><td class="option secondary">VIN Number:</td><td class="spec">'.$row['vin'].'</td></tr></table><div class="price"><b>Price:</b><br><div class="figure">$'.ltrim ($row['price'], '0').'<br></div><div class="tax">Plus Tax, Tag, and Title</div></div><div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> View Details </div><div class="clearfix"></div></a></div>';     
        }
?>
</div>

通过测试发送到变量$ q的输出,我得到的是'0'而不是'Audi'或'Ford'等.因此sql语句最终是SELECT * FROM库存WHERE make ='0'并且我需要它是SELECT * FROM库存WHERE make ='Audi'

With testing the output of what is sent to the variable $q I get '0' and not 'Audi' or 'Ford' etc. So the sql statement ends up being SELECT * FROM inventory WHERE make = '0' and I need it to be SELECT * FROM inventory WHERE make = 'Audi'

我希望这已经足够清楚了,我知道要解决如此看似很小的问题需要大量的信息,但是我从过去的经验中知道,提供的信息太少不仅令人沮丧,而且需要更多的时间来寻求适当的帮助.

I hope this has been clear enough and I know it's a lot of info to digest for such a seemingly small issue but I know from past experience that too little information given is not only frustrating but takes much more time for proper help.

谢谢.

推荐答案

我已修复它.因此,当php设置$ q时,它使用的是intval()函数,该函数将字符串替换为0.删除该函数将允许字符串通过并正确加载页面!

I fixed it. So when the php was setting the $q it was using intval() function which was overriding the string with 0. Removing that function allowed the string through and loaded the page correctly!

我更正后的php代码是:

My corrected php code is:

<?php
    include_once ('inc.dbh.php');
    $q = $_GET['q'];
    $inventorysql = "SELECT * FROM inventory WHERE make = '".$q."'";
    $result = mysqli_query($conn, $inventorysql);
        while ($row = $result->fetch_assoc()) {
            echo '<div id="inventory" class="inventory margin-bottom-20 clearfix scroll_effect fadeIn"><a class="inventory" href="inventory-listing.php?vin=' . $row['vin'] . '"><div class="title">'.$row['year'].' '.$row['make'].' '.$row['model'].'</div><img src="images/inventory/'.$row['vin'].'-main.jpg" class="preview" alt="preview"><table class="options-primary"><tr><td class="option primary">Body Style:</td><td class="spec">'.$row['body'].'</td></tr><tr><td class="option primary">Drivetrain:</td><td class="spec">'.$row['drivetrain'].'</td></tr><tr><td class="option primary">Transmission:</td><td class="spec">'.$row['transmission'].'</td></tr><tr><td class="option primary">Mileage:</td><td class="spec">'.ltrim ($row['mileage'], '0').'</td></tr></table><table class="options-secondary"><tr><td class="option secondary">Exterior Color:</td><td class="spec">'.$row['exteriorcolor'].'</td></tr><tr><td class="option secondary">Interior Color:</td><td class="spec">'.$row['interiorcolor'].'</td></tr><tr><td class="option secondary">VIN Number:</td><td class="spec">'.$row['vin'].'</td></tr></table><div class="price"><b>Price:</b><br><div class="figure">$'.ltrim ($row['price'], '0').'<br></div><div class="tax">Plus Tax, Tag, and Title</div></div><div class="view-details gradient_button"><i class="fa fa-plus-circle"></i> View Details </div><div class="clearfix"></div></a></div>';     
        }
    echo $q;
?>

这篇关于Ajax数据库选择值返回"0"而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆