需求如下:
有表Test,其中有10个字段构成,如:字段1、字段2、字段3、字段4、字段5、字段6、字段7、字段8、字段9、字段10。
而现在在做更新时,在接收参数时,去进行匹配,如果只传递了字段1和字段2,则只更新这2个字段值;若10个字段都有传递则都进行更新。
求大侠们帮助,用精辟点的方式来解决这个需求。

解决方案 »

  1.   

    $where=array();
    if($str1){
         $where[]="字段1='$str1'";
    }
    if($str2){
         $where[]="字段2='$str2'";
    }$where = ( count( $where ) ? ' set ' . implode( ' , ', $where ) : '' );
    $sql="update `table` $where where.....";
      

  2.   


    $i = 0;
    $data = "update `{$table_name}` set ";
    foreach ($_GET as $k => $v)
    {
        if ($i != 0)
        {
            $data .= ',';
        }
        $data .= "{$k} = '{$v}'";
    }就是这么个思路,但是一定要校验好用户输入的数据.