function updatecols($table,$arr){
创新互联建站从2013年开始,先为百色等服务建站,百色等地企业,进行企业商务咨询服务。为百色企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
$sql = "update ".$table." set ";
$total = count($arr);
$i=1;
foreach($arr as $k=$v){
$sql .= $k."=".$v;
if($i$total){
$sql .= ",";
}
$i++;
}
return $sql;
}
echo updatecols("table1",array('col1'='123','col2'='345'));
thinkphp批量更新数据可以参考如下三种方法:
方法一:
//批量修改 data二维数组 field关键字段 参考ci 批量修改函数 传参方式
function batch_update($table_name='',$data=array(),$field=''){
if(!$table_name||!$data||!$field){
return false;
}else{
$sql='UPDATE '.$table_name;
}
$con=array();
$con_sql=array();
$fields=array();
foreach ($data as $key = $value) {
$x=0;
foreach ($value as $k = $v) {
if($k!=$field!$con[$x]$x==0){
$con[$x]=" set {$k} = (CASE {$field} ";
}elseif($k!=$field!$con[$x]$x0){
$con[$x]=" {$k} = (CASE {$field} ";
}
if($k!=$field){
$temp=$value[$field];
$con_sql[$x].= " WHEN '{$temp}' THEN '{$v}' ";
$x++;
}
}
$temp=$value[$field];
if(!in_array($temp,$fields)){
$fields[]=$temp;
}
}
$num=count($con)-1;
foreach ($con as $key = $value) {
foreach ($con_sql as $k = $v) {
if($k==$key$key$num){
$sql.=$value.$v.' end),';
}elseif($k==$key$key==$num){
$sql.=$value.$v.' end)';
}
}
}
$str=implode(',',$fields);
$sql.=" where {$field} in({$str})";
$res=M($table_name)-execute($sql);
return $res;
}
//测试
function test(){
$update_array=array();
for ($i=2; $i 7 ; $i++) {
$data=array();
$data['id']=$i;
$data['memeber_type']=2;
$data['memeber_type_state']=1;
$update_array[]=$data;
}
$res=$this-batch_update('yl_member',$update_array,id);
var_dump($res);
}
另一个思路你试试:
html:
input type="text" name="A[]" /
input type="text" name="B[]" /
input type="hidden" name="ids[]" value="{$id}" /
php:
?php
$a = $_POST['A'];
$b = $_POST['B'];
$ids = $_POST['ids'];
foreach($a as $k = $v) {
$sql = "update abc set a='{$v}', b='{$b[$k]}' where id='{$ids[$k]}'";
mysql_query($sql);
}