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

时间:2014-03-11 06:06:32

标签: php mysql

      $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);

在下面的代码中我试图通过给查询提供品牌价值来获取品牌ID ...但它只提供单一ID ....请任何人帮助我... 我的桌子是...... brand_master1(ID,BRAND_NAME)

1 个答案:

答案 0 :(得分:0)

试试这个$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')";

请注意这行代码"','"

不仅用,逗号附加元素,还用'单引号附加元素

现在这个

的输出是什么
brand_name in ('$Brand')"

输出

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

但在你的情况下它是

brand_name in ('a,b,c')

警告

不推荐使用mysql_ *函数,您必须查看此Why shouldn't I use mysql_* functions in PHP?