简体   繁体   English

如何使用php比较sql中的多个复选框值?

[英]how to compare mutiple checkbox values in sql using php?

      $Brand=$_POST['Brand'];
         if(isset($_POST['Brand']))
          {
          $Brand = implode(",", $_POST['Brand']);   
           } else {
            $Brand = "";
          }
       echo "brands are :" .$Brand;
       echo "<br>";
       $sql2= "SELECT * FROM brand_master1 WHERE brand_name in ('$Brand')";
       $result2 = mysql_query($sql2);
       $row = mysql_fetch_array($result2,MYSQL_BOTH);
       $bid=$row['id'];
       echo "brand id is:";      
        print_r($bid);

in the below code am trying to get ids of brands by giving brand values to the query.... but it is giving only single id.... please anyone help me... my tables are... brand_master1(id,brand_name) 在下面的代码中,我试图通过为查询提供品牌值来获取品牌ID。...但是它只给出了一个ID。...请任何人帮助我...我的表是... brand_master1(id,品牌)

Try this $Brand = implode("','", $_POST['Brand']); 试试这个$Brand = implode("','", $_POST['Brand']);

if(isset($_POST['Brand']))
      {
      $Brand = implode("','", $_POST['Brand']);   
       } else {
        $Brand = "";
      }
   echo "brands are :" .$Brand;
   echo "<br>";
   $sql2= "SELECT * FROM brand_master1 WHERE brand_name in ('$Brand')";

Note this line of code "','" 注意这行代码"','"

it is not only appending the elements with , comma but also with ' single quotes 它不仅在元素后面加上,逗号,而且还用'单引号引起来

Now what will be the output of this 现在这将是什么输出

brand_name in ('$Brand')"

OUTPUT 输出值

brand_name in ('a','b','c')

But in your case it was 但是在你的情况下

brand_name in ('a,b,c')

WARNING 警告

Also mysql_* functions are deprecated you must have to look at this Why shouldn't I use mysql_* functions in PHP? 另外不建议使用mysql_ *函数,您必须查看此内容为什么我不应该在PHP中使用mysql_ *函数?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM