我有一个二手车网站,该网站使用以下sql在页面上列出了其当前所有库存:“ SELECT * FROM库存订单按时间戳ASC”。
当下拉菜单的选项已更改时,我试图使用Ajax数据库操作的W3Schools(https://www.w3schools.com/php/php_ajax_database.asp)方法来过滤我的结果。
javascript和php都能正常运行,但是在新sql语句中发送并使用的值显示为“ 0”,而不是应发送的值。
此下拉菜单的值从同一张表中提取,以动态生成菜单,并且每个菜单的值都应为“ Ford”,“ Audi”,“ Dodge”等。在由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是:
<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代码为:
<?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是:
<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库存中的make ='Audi'
我希望这已经足够清楚了,而且我知道要解决如此看似很小的问题需要大量的信息,但我从过去的经验中知道,提供的信息太少不仅令人沮丧,而且需要更多的时间来寻求适当的帮助。
谢谢。
答案 0 :(得分:1)
我修复了它。因此,当php设置$ q时,它使用的是intval()函数,该函数将字符串替换为0。删除该函数将允许字符串通过并正确加载页面!
我更正后的php代码是:
<?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;
?>