<?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();//关闭数据连接 ?>
1