0

I already can update my data from database but the problem is i just want to update one data, but when I enter the new data it updated all of data in the database table

like this enter image description here

this is View:

<form method="post" action="<?php echo base_url() ?>user/update_user_data">

<div class="form-group">
    <label> Enter New first name</label>
    <input type="text" name="firstname" class="form-control" />
</div>
<div class="form-group">
    <label> Enter New last name </label>
    <input type="text" name="lastname" class="form-control" />
</div>
<div class="form-group">
    <label> Enter New fullname </label>
    <input type="text" name="fullname" class="form-control" />


<div class="form-group">  
    <input type="submit" name="update" value="Update" class="btn btn-info" />
</div>

this is model:

function update_data($id = FALSE)  
  {  

    if($id == FALSE){
            $query = $this->db->get('user');
            return $query->result_array(); 
        }

        $query = $this->db->get_where('user', array('id' => $id));
        return $query->row_array();
  } 
 public function update_user_data($data)
    { 
        $data = array('firstname' => $this->input->post('firstname'),
                        'lastname' => $this->input->post('lastname'),
                        'fullname' => $this->input->post('fullname')


                      );

        $this->db->update("user", $data);
    }

this is controller:

    public function update_data($id = NULL)
    {

        $data['us'] = $this->User_Model->update_data($id);

        if (empty($data['us'])) {
            show_404();
        }
         $this->load->view('user/update-user', $data);
    }

    public function update_user_data()
    {
        $data['title'] = 'Update user';

            $this->load->library('upload');
                $data =  array('upload_data' => $this->upload->data());
            $this->User_Model->update_user_data($data);

            redirect('user/index');

    }

Im just a beginner please respect thanks If there is something wrong with my code please let me know. Its working by the way, the only problem is once I update the users data it updating all of the data in the database.

4
  • you're updating the whole table not inserting Commented Mar 22, 2019 at 13:10
  • 1
    You should add the ID of the row you want to update to the call Commented Mar 22, 2019 at 13:22
  • for update query need where condition,use id in where condition Commented Mar 23, 2019 at 6:45
  • and by the way @Eduards, yes im 'updating' not 'inserting'. My problem is instead of updating 1 row its updating all of my data. Commented Mar 23, 2019 at 9:06

1 Answer 1

1

You are modifying every row in the table because of you have not given any specific row. And for this reason every row is updated after update code runs. you should use below code

function update_data($id = FALSE)  
  {  

    if($id == FALSE){
            $query = $this->db->get('user');
            return $query->result_array(); 
        }

        $query = $this->db->get_where('user', array('id' => $id));
        return $query->row_array();
  } 
 public function update_user_data($data)
    { 
        $data = array('firstname' => $this->input->post('firstname'),
                        'lastname' => $this->input->post('lastname'),
                        'fullname' => $this->input->post('fullname')


                      );

        $this->db->where('id','1')->update("user", $data); //change id as require your own need
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.