php更新数据库多个值 php更新数据库多个值怎么操作

php如何用update更新多条数据楼主贴出的代码是主要代码不是完整代码 , 以下是对你贴出代码的修改,接收数组并批量更新
接收数据格式形如:
array(
0=array(
'id' = 1,
'title'= '标题1'
'url'= '...'
),
1=array(
'id' = 1,
'title'= '标题1'
'url'= '...'
),
)
注意在原来的基础上加了更新条件id,不加的话是会更新全部的 。
A页面代码:
?php
$exec="select * from focusimg";
$result = mysql_query($exec,$link);
$i=0;
while($pt=mysql_fetch_array($result)){
?
tr onmouseover="this.className='admTr'" onmouseout="this.className=''"
td width="17%" align="right"标题?php echo $pt['id']; ?:input name="?php echo "focusimg[$i][id]"; ?" value="https://www.04ip.com/post/?php echo $pt['id']; ?" type="hidden"//td
tdinput name="?php echo "focusimg[$i][title]"; ?" type="text" id="textfield" value="https://www.04ip.com/post/?php echo $pt['title'];?" size="30" //td
td width="17%" align="right"链接?php echo $pt['id']; ?:/td
tdinput name="?php echo "focusimg[$i][url]"; ?" type="text" id="textfield" value="https://www.04ip.com/post/?php echo $pt['url'];?" size="30" / /td
/tr
?php
$i++;
};
?
B页面代码:
?php
include 'conn.php';
$focusimg=$_POST['focusimg'];
foreach($focusimg as $k=$v) {
$exec="update chips set title='{$v[title]}',url='{$v[url]}' where id='{$v[id]}'";
mysql_query($exec, $link);
}
【php更新数据库多个值 php更新数据库多个值怎么操作】mysql_close($link);
echo "edit OK!";
?
PHP中如何更新大量的mysql数据sql= "UPDATE mydata SET p1='".$pointarr[i][1]."' where ps like '%".$pointarr[i][
每次都去做一次likephp更新数据库多个值的全表扫描当然慢了 。
建议使用批量更新php更新数据库多个值,减少查询次数 。
比如先查询出结果集,然后在内存里去操作字段更新
最后采用 update ...... where id = xxx 这样php更新数据库多个值的更新方法 。避免多次全表扫描 。
不是最了解php更新数据库多个值你php更新数据库多个值的需求,仅供参考 。
Thinkphp怎么批量更新数据thinkphp批量更新数据可以参考如下三种方法:
方法一:
//批量修改 data二维数组 field关键字段 参考ci 批量修改函数 传参方式
function batch_update($table_name='',$data=https://www.04ip.com/post/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=https://www.04ip.com/post/array();
$data['id']=$i;
$data['memeber_type']=2;
$data['memeber_type_state']=1;
$update_array[]=$data;

推荐阅读