gpt4 book ai didi

php - 尝试使用查询从数据库中过滤数据。为什么这行不通?

转载 作者:搜寻专家 更新时间:2023-10-30 23:34:38 25 4
gpt4 key购买 nike

我正在尝试从我的数据库中过滤车辆。我希望客户能够通过选择价格范围字段和汽车类别来过滤车辆....类别在我的数据库中名为类别的表中,车辆是从我的车辆表中调用的数据库。下面是我的代码。非常感谢任何想法。

<div class="main">

<form method="post" action='<?php echo $_SERVER['PHP_SELF']; ?>' >
<select name="price">
<option value="= price">All</option>
<option value="< 5000">$0 - $5,000</option>
<option value="> 5000 AND price < 10000">$5,000 - $10,000</option>
<option value="> 10000 AND price < 20000">$10,000 - $20,000</option>
<option value="> 20000 AND price < 50000">$20,000 - $50,000</option>
<option value="> 50000">$50,000+</option>
</select>
<?php
// Read the values in from the department table to populate the dropdown
$query = "SELECT * FROM category";
$results = mysqli_query($conn, $query );
if(!$results) {
echo ("Query error: " . mysqli_error($conn));
}
?>

<select name="category">
<option value="All">All</option>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<option value="<?php echo $row['cat_id'];?>">
<?php echo $row['category_description'];?> </option>
<?php } ?>
</select>


<?php
//set initial value for sort variable
$price = "= price"; // this will set the default value for the price filter to all
$category = "%"; // this will set the default value of the category filter return all values

if(isset($_POST['price'])) {
$price = $_POST['price'];
}
if(isset($_POST['category'])) {
$category = $_POST['category'];
}
?>
<input type="submit" name="update" value= "Display results">
</form>
<?php
// Determine which button was pressed and call the appropriate file
if (isset($_POST['price'])) {
//set query variable
$query = "SELECT * FROM vehicles WHERE price = price AND category_id LIKE %";
}
elseif (isset($_POST['category'])) {
$query = "SELECT * FROM vehicles WHERE price $price AND category_id LIKE '$category'";
}

$query = "SELECT * FROM vehicle WHERE standardusedvehicle ='yes'";
$results = mysqli_query($conn, $query );
if(!$results) {
echo ("Query error: " . mysqli_error($conn));
}

else {
// Fetch and display the results of the select query
while ($row = mysqli_fetch_array($results)) {
echo "<p>VIN_#: $row[vin]</p> ";
echo "<p>Stock Number: $row[stockno]</p> ";
echo "<p>Manufacturer Number: $row[man_num]</p>";
echo "<p>Model: $row[model]</p>";
echo "<p>Colour: $row[col_id]</p>";
echo "<p>Year: $row[year]</p>";
echo "<p>Price: $row[price]</p>";
echo "<p>Kilometres: $row[kms] </p>";
echo "<p>Registration: $row[rego] </p>";
echo "<p>Cylinders: $row[cylinders] </p>";
echo "<p>Fuel: $row[fuel] </p>";
echo "<p>Transmission: $row[transmission] </p>";
echo "<p>Category Id: $row[cat_id] </p>";
echo "<p>Vehicle on Special (yes/no): $row[special] </p>";
echo "<p>Standard Used Vehicle: $row[standardusedvehicle] </p>";
echo "<img src='{$row['vehicle_image']}' />";
}
}
?>

最佳答案

在此代码中,您使用了两个表“vehicle”和“vehicles”。请使用正确的那个。

请在表单标记结束后使用此代码,而不是此代码:

if (isset($_POST['price'])) {
//set query variable
$query = "SELECT * FROM vehicles WHERE price = price AND category_id LIKE %";
}
elseif (isset($_POST['category'])) {
$query = "SELECT * FROM vehicles WHERE price $price AND category_id LIKE '$category'";
}

$query = "SELECT * FROM vehicle WHERE standardusedvehicle ='yes'";

请将上面的代码改成下面的代码:

if (isset($_POST['price'])) {
if($_POST['category'] != 'All'){
$query = "SELECT * FROM vehicles WHERE price $price AND category_id = $category";
}else {
$query = "SELECT * FROM vehicles WHERE price $price";
}
}

我不知道你要不要下面这行。

$query = "SELECT * FROM vehicle WHERE standardusedvehicle ='yes'";

如果你想要上面这一行,请使用下面的代码代替上面的代码:

 if($_POST['category'] != 'All'){
$query = "SELECT * FROM vehicles WHERE price $price AND category_id = $category AND standardusedvehicle ='yes' ";
}else {
$query = "SELECT * FROM vehicles WHERE price $price AND standardusedvehicle ='yes'";
}
}

关于php - 尝试使用查询从数据库中过滤数据。为什么这行不通?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com