动态创建mysql选择查询 [英] Dynamically Creating mysql select Query

查看:71
本文介绍了动态创建mysql选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站中有多个复选框:
复选框1
复选框2
复选框3等。

I have multiple checkboxes in my websites: checkbox 1 checkbox 2 checkbox 3 etc.

我想动态根据上面的复选框生成mysql查询。

I want to dynamically generate mysql query based on the above checkboxes.

i:e如果第一个复选框被选中,那么查询应该是:

i:e if 1st checkbox is selected then the query should be:

$mysql="Select * from mytable where colname=" . $checkbox1 .;

如果第一个和第二个复选框被选中,那么查询应该是:

if 1st and 2nd checkbox is selected then the query should be:

$mysql="Select * from mytable where colname=" . $checkbox1 ."AND colname=" . $checkbox2 ." ;

如果全部选中,那么应该是:

if all are selected then it should be :

$mysql="Select * from mytable where colname=" . $checkbox1 . "AND colname=" . $checkbox2 ."AND colname=" . $checkbox3 .  ;

有人可以帮忙:

Can someone pls help:

推荐答案

你必须改变你的表格,因为它采取多个值应该作为数组发布

you have to change your form like follow because its taking multiple value its should be post as an array

<form action="register.php" method="POST"> 
  <input type="checkbox" name="rating[]" value="5">5 Star 
  <input type="checkbox" name="rating[]" value="4">4 Star 
  <input type="checkbox" name="rating[]" value="3">3 Star 
  <input type="checkbox" name="rating[]" value="2">2 Star 
  <input type="checkbox" name="rating[]" value="1">Less than 2 Star 
</form>

然后在php中

  $where = '';
   if(isset($_POST['rating'])){
     $data = implode(',',$_POST['rating']); // beacuse your rating is only one column in db i think
     $where = "WHERE cloumn_name IN($data)";
   }
  $query = "SELECT * FROM your_table $where";

这篇关于动态创建mysql选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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