$wd1=$_POST['wd1']; //原来是这样
sql="select * from xxx where pr_id like '%". $wd1."%'";
//**********************************************************
//现在要做成假如输入:123 王
//sql="select * from xxx where pr_id like '%123%' and name like '%王%'";
//等等就像上面这样,用户输入的条件可能是多个如:123 王 男或者一个
wd1=$_POST['wd1']; //现在接收input数据,
$split_dir = split ('[ ]',$wd1);//我想着转换成数组
//然后sql server2000语句不知道咋写了,不知道我的意思表达明白了没有,我想表达清楚,求解答思路,
sql="???";
PHPSQL Server

解决方案 »

  1.   


    $where = "1=1";
    $arr = array('col1','col2','col3');
    foreach($split_dir AS $key=>$val){
    $where .= " AND ".$arr[$key]." LIKE '%".$val."%' ";
    }$arr是需要在where里面出现的字段名,当然了,安全方面的事你自己控制。这个只是个大概的思路,这个很显然不够完善,因为别人可能输入 123 男 王   或者增加输入之间的空格,这个都需要你自己去控制
      

  2.   

    嘿嘿好了。。我是这样写的
           $wd1=$_POST['wd1']; 
                                    $split_dir = split ('[ ]',$wd1);  

    $arr = array('编码','名称','类别','型号');
    $where2=" ";
    for($m=0;$m<count($split_dir);$m++){
    if($m==(count($split_dir)-1))
    {
    for($n=0;$n<count($arr);$n++){

    if($n==(count($arr)-1)){
    $where2=$where2.$arr[$n]." like '%".$split_dir[$m]."%'";
    }else{
    $where2=$where2.$arr[$n]." like '%".$split_dir[$m]."%' or ";
    }

    }
    $where2=$where2." ";
    }else{
    for($n=0;$n<count($arr);$n++){

    if($n==(count($arr)-1)){
    $where2=$where2.$arr[$n]." like '%".$split_dir[$m]."%'";
    }else{
    $where2=$where2.$arr[$n]." like '%".$split_dir[$m]."%' or ";
    }

    }
    $where2=$where2." ) and ( ";
    }
    }