function save($tn,$data=array()){
global $m;
$ty = '';
foreach($data as $k=>$v){
$kk[] = $k;
$vv[] = '?';
$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);
$value = implode(',',$vv);
$stmt = $m->prepare("insert into $tn($key) values($value)");
eval($bind);
$stmt->execute();
$stmt->free_result();
}
插入数据预处理语句函数的使用
//方法一修改数据也是同样的方法只是多了一个条件
$d['sname']='你bu好';
$d['sscore']=90;
$d['sgender']='保密';
save('stu',$d);
//方法二
save('stu',array('sname'=>'好人'));