mysql查询多选项商品查询,在选择框中选择多个选项并从数据库获取结果?

I have made a webpage for a school project where I have a series of selection boxes (some are independent and some are depended on another) to make selections and then apply filter to make a query. It is still in test-mode and working fine for single selections. You can find the webpage here: http://gorevler.awardspace.biz/realdeal03.html

For example, when I select Europe from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter it gives out the result.

But, when I select Europe AND North America from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter, it gives only one result. but there are two records matching this filter in the database. So it does not recognise my multiple selection. Any advice about how I could make it work so that when I make multiple selection on a box and apply filter, it gives all the results including all the selection?

I guess it is something about the PHP coding, and I tried different things but no success yet. Since I am not a programmer, I don't seem to understand where the problem lays. Any advice about the coding would be appreciated.

解决方案

Is it because when you select two regions your query becomes:

SELECT ... FROM ... WHERE region ='North America' AND region = 'Europe' which means you will never get any results from the database as the region cannot be both? If so you'll need to make it so you use "OR" statements for multiple selected regions.

There are numerous ways of completing this. First of all you need a way of dealing with multiple inputs for your region, rather than just defining the one input in your php code. Basically once you have capture your multiple regiones then your query line is the only line that needs to change. This can become:

$sql = mysql_query("SELECT * from gorevler WHERE region IN ('%$bolge%','%$bolge2%','%bolge3%') AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");

Or

$sql = mysql_query("SELECT * from gorevler WHERE (region = '$bolge' OR region = '$bolge2' OR region = '$bolge3) AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");

EDIT with some pointers on your last question:

It just needs to come from your HTML. So if you select more than one region then it will post these to your PHP page. For example:

Europe

USA

then in your PHP

$bolge=$_POST['europe];

$bolge2=$_POST['usa'];

etc.

That's probably the quick and dirtiest method anyway.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值