Blogger Information
Blog 34
fans 0
comment 0
visits 28425
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
425MySQLi面向对象实现更新操作
1A7498的博客
Original
725 people have browsed it
<?php
require 'mysqli_connect.php';
$sql = "UPDATE IGNORE staff SET name=?,salary=? WHERE staff_id=?;";
$stmt = $mysqli->stmt_init();
if($stmt->prepare($sql)){
	$data[] = ['name'=>'4hao0','salary'=>110,'staff_id'=>4];
	$data[] = ['name'=>'5hao0','salary'=>120,'staff_id'=>5];
	$data[] = ['name'=>'6hao0','salary'=>130,'staff_id'=>6];
	$stmt->bind_param('sii',$name,$salary,$staff_id);//绑定参数,此时变量$name都不存在
	foreach($data as $staff){//循环数组
		$name = $staff['name'];//将数组中的name值赋予$name
		$salary = $staff['salary'];
		$staff_id = $staff['staff_id'];
		$stmt->execute();//执行sql语句
	    if($stmt->affected_rows>0){
		echo '<br>更新成功'.$stmt->affected_rows.'条记录,id:'.$staff_id;
	    }else{
	    echo '<br>没有新增记录';
	    }
	}
}else{
	exit($stmt->errno.':'.$stmt->error);//输出错误信息
}
$mysqli->close();//关闭数据连接
?>

25gengxin.png

gengxin2.png







1



Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
  • 2018-03-16 11:39:01