简体   繁体   English

如何根据用户使用 php 选中的所有复选框查询数据库?

[英]How do i query the database based on all the checkboxes checked by the user using php?

An image of the table is here.一张桌子的图片在这里。 I need to access all the checkboxes checked by the user and query database based on that.我需要访问用户检查的所有复选框并基于此查询数据库。 For Example : if user checks honda and nissan in brands , 3-3.5 and 3.5-4 in price and 15-20 and 20-25 in mileage , i need to query the DB and give the results which satisfy all the above conditions.例如:如果用户检查品牌的本田和日产,价格为 3-3.5 和 3.5-4,里程数为 15-20 和 20-25,我需要查询数据库并给出满足上述所有条件的结果。 How do i implement this in php?我如何在 php 中实现它? I have used Mysql database and it has 5 columns namely brand, mileage,fuel type , price and modelID(primary key).我使用过 Mysql 数据库,它有 5 列,分别是品牌、里程、燃料类型、价格和模型 ID(主键)。

<form method = "post">
<h1>BRAND</h1>
<font size="5">
<table cellspacing = "20px">
<tr>
<td>Hyundai<input type = "checkbox" name = "Hyundai"></input></td>
<td>Honda<input type = "checkbox" name = "Honda"></input></td>
<td>Ford<input type = "checkbox" name = "Ford"></input></td>
<td>Nissan<input type = "checkbox" name = "Nissan"></input></td>
<td>Volkswagen<input type = "checkbox" name = "Volkswagen"></input></td>
<td>Chevrolet<input type = "checkbox" name = "Chevrolet"></input></td>
<td>TATA Motors<input type = "checkbox" name = "TATA Motors"></input></td>
</tr>
</table>
</font>

<h1>MILEAGE(kmpl)</h1> 
<font size="5">
<table cellspacing = "20px">
<tr>
<td>15-20<input type = "checkbox"></input></td>
<td>20-25<input type = "checkbox"></input></td>
<td>25-30<input type = "checkbox"></input></td>
<td>30-35<input type = "checkbox"></input></td>
<td>35-40<input type = "checkbox"></input></td>
</tr>
</table>
</font>


<h1>PRICE(in lakhs)</h1>
<font size="5">
<table cellspacing = "20px">
<tr>
<td>2 - 2.5<input type = "checkbox"></input></td>
<td>2.5 - 3<input type = "checkbox"></input></td>
<td>3 - 3.5<input type = "checkbox"></input></td>
<td>3.5 - 4<input type = "checkbox"></input></td>
<td>4 - 4.5<input type = "checkbox"></input></td>
<td>4.5 - 5<input type = "checkbox"></input></td>
<td>5 - 5.5<input type = "checkbox"></input></td>
</tr>
</table>
</font>

<h1>FULE TYPE</h1>
<font size="5">
<table cellspacing = "20px">
<tr>
<td>PETROL<input type = "checkbox"></input></td>
<td>DIESEL<input type = "checkbox"></input></td>
<td>CNG<input type = "checkbox"></input></td>
</tr>
</table>
</font>
<input type = "submit" value = "GET ME RESULTS" ></input>
</form>

The first problem you'll run into is the way your inputs are set up.您将遇到的第一个问题是输入的设置方式。 If you write the checkbox HTML like this, it will be a lot easier to handle on the PHP side.如果像这样编写复选框 HTML,那么在 PHP 端处理起来会容易很多。

<td>Hyundai<input type="checkbox" name="brand[]" value="Hyundai"></input></td>
<td>Honda<input type="checkbox" name="brand[]" value="Honda"></input></td>
...etc.

<td>15-20<input type="checkbox" name="mileage[]" value="15-20"></input></td>
<td>20-25<input type="checkbox" name="mileage[]" value="20-25"></input></td>
...etc

This way, in the script that handles the form submission, you can refer to $_POST['brand'] , for example, and get an array of all the selected brands.这样,在处理表单提交的脚本中,您可以参考$_POST['brand'] ,例如,并获取所有选定品牌的数组。

An important thing when writing your query will be how to combine the ANDs and ORs.编写查询时的一件重要事情是如何组合 AND 和 OR。 You should group your ORs in parentheses to make sure it functions the way you intend it to.您应该将您的 OR 分组在括号中,以确保它按您预期的方式运行。 For example:例如:

WHERE (brand='Hyundai' OR brand='Honda') AND (mileage='15-20' OR mileage='20-25')

A cleaner way to do this is to use IN instead of a bunch of ORs一种更简洁的方法是使用 IN 而不是一堆 OR

WHERE brand IN ('Hundai', 'Honda') AND mileage IN ('15-20', '20-25')

As for as the PHP code for how to write the interaction with your database, that really is too broad of a topic to answer, but you said you weren't asking for PHP code, so hopefully this will give you a general idea that will get you on your way.至于如何编写与数据库交互的 PHP 代码,这个话题实在是太广泛了,无法回答,但是您说您不是在要求 PHP 代码,所以希望这会给您一个总体思路让你上路。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据选中的复选框选择多个mysql记录? - how do i select multiple mysql records based on checked checkboxes? 如何使用PHP获取已选中复选框的名称并将其存储在数据库中 - How to get the name of checked checkboxes and store them in a database with using php 如何检查 PHP 中的所有复选框是否已选中? - How can I check if all checkboxes have been checked in PHP? 如何使用/不使用foreach一次获取选中的复选框的多个值? p - How do I get multiple values of of checked checkboxes at once with/without using foreach? Php 如何在PHP中查询数据库并根据匹配的用户输入返回结果? - How do I query a database in PHP and return results based on matching user-input? 我如何获得从数据库获取所有值并作为复选框工作的输出的php语句? (PHP / MySQL) - How do I get this php statement that gets all the values from database and outputs as checkboxes to work? (PHP/Mysql) 获取所有选中复选框的值并将它们插入数据库 - PHP - Get the value of all checked checkboxes and insert these to the database - PHP 如何回显已选中的复选框? - How do I echo my checked checkboxes? 如何使用php和mysql插入选中的复选框并删除未选中的复选框? - How to Insert checked checkboxes and delete unchecked checkboxes using php and mysql? 使用 PHPMailer 根据用户选中的复选框向用户发送附件? - Using PHPMailer to send attachments to the user based on the checkboxes they have checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM