1

$_POST['domain']是具有 3 个或更多域值(C、C++、Java 等)的某种形式的 CHECKBOX 的结果,其值现在保存在一个数组'$Domain'中。现在,我需要从表中选择与用户在 CHECKBOX 中选择的域相同的元组。

我尝试使用 FOR 循环,但它给了我一个 T_FOR 无异常错误或其他东西。现在我正在尝试使用 implode 函数,它没有给出任何查询错误,但也没有显示任何查询结果!问题位于查询的 IN 运算符中。我该如何解决?

if(isset($_POST['domain']))
    {
        $Domain = $_POST['domain'];
        $search=$_POST['search'];

        $N = count($Domain);

        echo("<p>You selected $N domain(s):<br/><br/> ");
        for($i=0; $i < $N; $i++)
        {
            echo($Domain[$i] . "<br/> ");
        }
        echo("</p>");

        $dom_str="'".implode("', '",$Domain)."'";

    $query ="SELECT * FROM learner_object WHERE(lo_tag1='$search' || lo_tag2='$search' || lo_tag3='$search') AND 
            lo_domain IN ( ".$dom_str." ) ";

    $result =mysql_query($query)
    or die('query failed.'.mysql_error());

      while($row = mysql_fetch_array($result))
        {   
            echo  "<a href=\" ".$row['lo_url']." \" target=\"_blank\" \">"  .   $row['lo_name']  .   "</a> <br/> ";     
            echo $row['lo_desc']."<br/><br/>";      
        }
     }
     else
     echo "<p>You didn't select any Domain.</p><br/>";
4

3 回答 3

1

试试这个,

  $dom_str=!empty($Domain)?implode(",",$Domain):'default value';

$query ="SELECT * FROM learner_object WHERE(lo_tag1='$search' || lo_tag2='$search' || lo_tag3='$search') AND 
        lo_domain IN ($dom_str)";
于 2012-10-16T17:18:15.770 回答
0
$dom = implode (',',$Domain);  
$query ="SELECT * FROM learner_object WHERE(lo_tag1='$search' || lo_tag2='$search' || lo_tag3='$search') AND 
        lo_domain IN ( ".$dom." ) ";
于 2012-10-16T17:21:09.570 回答
0

感谢你们帮助我。在您的帮助下,我找到了正确的代码。询问:-

$query ="SELECT * FROM learner_object WHERE(lo_tag1='$search' || lo_tag2='$search' || lo_tag3='$search') AND lo_domain IN ( '" . implode("','", array_map('trim', $Domain)) ."' ); ";
于 2012-10-17T05:41:12.097 回答