|
大佬们我又来啦!
我的项目用的是sqlite3的数据库,ci是3.1.2,使用说明手册里的insert和query一直都没有问题,但是今天写个人管理,想要更新用户信息的时候,update会把整张表的数据都更新,不想用replace,因为不想每次都把全部字段更新一遍,希望可以更新其中的部分字段,但是使用update时,
$this->db->query()选出对应行(也可以证明只选了一行)
$this->update->('user',$data)会把整张表的所有数据都更新,这是为毛啊,希望大佬能解答一下
代码贴上来
controller
PHP复制代码
[color=#000080]function index(){
[/backcolor][backcolor=#f7faff] $this->load->model('db_model');
[/backcolor][backcolor=#f7faff] $users = $this->db_model->get_data();
[/backcolor][backcolor=#f7faff] foreach($users as $user){
[/backcolor][backcolor=#f7faff] echo $user->username;
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff] $name = $user->username;
[/backcolor][backcolor=#f7faff] $id = $user->userid;
[/backcolor][backcolor=#f7faff] $password = $user->password;
[/backcolor][backcolor=#f7faff] $avatar = $user->avatar;
[/backcolor][backcolor=#f7faff] $sex = $user->sex;
[/backcolor][backcolor=#f7faff] $city = $user->cityid;
[/backcolor][backcolor=#f7faff] }
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff] $data = array(
[/backcolor][backcolor=#f7faff] 'username'=>'啊扣扣',
[/backcolor][backcolor=#f7faff] 'password'=>$password,
[/backcolor][backcolor=#f7faff] 'avatar'=>$avatar,
[/backcolor][backcolor=#f7faff] 'sex'=>$sex,
[/backcolor][backcolor=#f7faff] 'cityid'=>$city
[/backcolor][/color][color=#660000] );
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff] $this->db_model->update($id,$data);
[/backcolor][backcolor=#f7faff] $users = $this->db_model->get_data();
[/backcolor][backcolor=#f7faff] foreach($users as $user){
[/backcolor][backcolor=#f7faff] echo $user->username;
[/backcolor][backcolor=#f7faff] }
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff]}
复制代码
model
PHP复制代码
[color=#000080]public function get_data(){
[/backcolor][backcolor=#f7faff] $query = $this->db->get('user');
[/backcolor][backcolor=#f7faff] return $query->result();
[/backcolor][backcolor=#f7faff]}
[/backcolor][backcolor=#f7faff]
[/backcolor][color=#000080]public function update($id, $data){
[/backcolor][backcolor=#f7faff]
[/backcolor][backcolor=#f7faff] $query=$this->db->query("select * from user WHERE userid='".$id."'");
[/backcolor][backcolor=#f7faff] echo "<br />";
[/backcolor][backcolor=#f7faff] echo $query->num_rows();
[/backcolor][backcolor=#f7faff] echo "<br />";
[/backcolor][backcolor=#f7faff] $this->db->update('user',$data);
[/backcolor][backcolor=#f7faff]}
复制代码
最后输出
啊扣扣张耳朵张狗蛋张全蛋
1
啊扣扣啊扣扣啊扣扣啊扣扣
第一排是数据库里原来的username
中间是选出来的行数
最后一排是最终改完以后数据库里的username
|
|