$tn 表名,$data为修改要修改的数据,$w为条件
function update($tn,$data=array(),$w='1=1'){
global $m;
$ty = '';
foreach($data as $k=>$v){
$kk[] = $k.'=?';
$dd[] = "$data['$k']";
switch(gettype($v)){
case 'integer':$ty.='i';break;
case 'string':$ty.='s'; break;
case 'double':$ty.='d';break;
default:$ty.='b';break;
}
}
$vvv = implode(',',$dd);
$bind = "$stmt->bind_param('$ty',$vvv);";
$key = implode(',',$kk);
$stmt = $m->prepare("update $tn set $key where $w");
eval($bind);
$stmt->execute();
$stmt->free_result();
$stmt->close();
}
预处理语句修改数据函数的使用
update('stu',array('sname'=>'钻石王老五','sscore'=>100),'sid=7');