兄弟们,我现在正在更新一个表单里的一列数据。我选择了200行,然后用foreach逐个循环。但是发现不但更新了我选择的200行,连其余的3000行都更新了。希望帮忙看下!
<code>$this->db->where('weekday', 5); $this->db->where('source', 'site'); $record = $this->db->get('user', 200); print_r($record->num_rows()); foreach ($record->result() as $row) : $data = array( 'weekday' => 1, ); $this->db->where('user_id', $row->user_id); $this->db->update('user', $data); endforeach; </code>
提取的$record数量已通过print_r验证确实为200个。
谢谢!
更新:
在endforeach之后加上$this->db->last_query();试过了。返回结果是:UPDATE user SET weekday = 1。貌似没有包括和$record相关的条件。哪里出了问题呢?
更新:
以下兄弟们提供的回答多次试过都不行。大家可不可以帮忙想下有什么不用foreach的其他方法?
回复内容:
兄弟们,我现在正在更新一个表单里的一列数据。我选择了200行,然后用foreach逐个循环。但是发现不但更新了我选择的200行,连其余的3000行都更新了。希望帮忙看下!
<code>$this->db->where('weekday', 5); $this->db->where('source', 'site'); $record = $this->db->get('user', 200); print_r($record->num_rows()); foreach ($record->result() as $row) : $data = array( 'weekday' => 1, ); $this->db->where('user_id', $row->user_id); $this->db->update('user', $data); endforeach; </code>
提取的$record数量已通过print_r验证确实为200个。
谢谢!
更新:
在endforeach之后加上$this->db->last_query();试过了。返回结果是:UPDATE user SET weekday = 1。貌似没有包括和$record相关的条件。哪里出了问题呢?
更新:
以下兄弟们提供的回答多次试过都不行。大家可不可以帮忙想下有什么不用foreach的其他方法?
Problem solved. You cannot limit what the update function does. It will do the update for all rows in the table despite the foreach function. In order to accomplish the same objective, I've resorted to:
<code> $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200"; $this->db->query($update); $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200"; $this->db->query($update); $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200"; $this->db->query($update); $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200"; $this->db->query($update); ...... all the way to the end. Of course I used a while loop to do the above. </code>
你的user_id不是uniq_key?
建议你跟到db这个对象里,把sql打印出来看一下,确认是否由于最终update的SQL条件出了问题
怀疑是$this->db的使用有问题,上面你有db做查询,下面又直接拿来update,总感觉不太对
你这样试试:
<code>$this->db->update('user',$data,array('user_id'=>$row->user_id)); </code>
再不行的话,就别用替代语法了,直接写foreach
假如你的 user_id 是自增的,试试这个看看可以不。
$query = $this->db->get_where('user', array('weekday' => 5, 'source' => 'site'), 200); $result = $query->result(); $lastRecord = array_pop($result); $data = array( 'weekday' => 1, ); $this->db->update('user', $data) ->where('weekday', 5) ->where('source', 'site') ->where('user_id > ', $lastRecord->user_id - 1);
好奇怪的语法,没见过。不过我觉得是foreach最后的update有问题。试试这样:
<code>$this->db->update('user', $data)->where('user_id', $row->user_id); </code>
楼主这用的是CI么?单纯看这看循环应该是没有问题的。
这了个脚本测试也没有问题。
$record = $this->db->get('user',10); $data = array( 'resetpwd' => 5, ); //这个最好还是放在循环外面 foreach ($record->result() as $rows): $this->db->where('userid',$rows->userid); $this->db->update('user',$data); //print_r($rows); print_r($this->db->last_query()); echo '<br \/>'; endforeach;
输出
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '1'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '2'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '3'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '4'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '5'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '6'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '7'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '8'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '9'
UPDATE m_user
SET resetpwd
= 5 WHERE userid
= '10'
楼主可能需要提供更多信息。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
