简体   繁体   中英

Update a database record using codeigniter

I have been having a problem... a frustrating problem at that. I cannot seem to edit a specific row in Codeigniter . I have found previous questions on the same here, and even tried the solutions but to no avail. I am press for time on this project I am undertaking. Before you regard this question as a duplicate please have a look see. Any help will be greatly appreciated... Thank you in advance My code snippets are below:

Codeigniter/Controller

<?php..

    //Selects all from admin table
        function get_admin(){
                $data['query'] = $this->Superuser_Model->selectadmin();
        }
        //brings in the view
        // function editAdmin(){
        //  $data['content'] = 'admin/edit_admin';
        //  $this->load->view('include/template_back', $data);
        // }
        //click a specific row in the view (tabulated data)
        function edit($id){
            $data['array']= $this->Superuser_Model->editadmin($id);         
            // $data['content'] = 'admin/edit_admin';
            $this->load->view('include/header_back');
            $this->load->view('admin/edit_admin', $data);
            $this->load->view('include/footer_back');
        }
        //Should update the from
        function update_superuser(){
            $this->form_validation->set_rules('username','Username','required');
            $this->form_validation->set_rules('password','Password','required');
            if($this->form_validation->run()==FALSE)
            {
                $data['content'] = 'admin/add_admin';
                $this->load->view('include/template_back', $data);
            }
            else
            {
                $username = $this->input->post('username');
                $password = md5($this->input->post('password'));
                $date_added = $this->input->post('date_added');
                $this->Superuser_Model->update_superuser($username,$password,$date_added);
                redirect('login/index', 'refresh');
            }

        }
..?>

Codeigniter/Model

    <?php...
    function selectadmin(){
            // $id = $this->uri->segment(3);
            $query = $this->db->get('admin');       
            return $query->result_array();
        }
        function editadmin($id){
            $id = $this->uri->segment(3);
            $query = $this->db->get('admin');
            $this->db->where('adminID', $id);       
            return $query->result_array();
        }
        function update_superuser($data, $id){
            $this->uri->segment(3);
            $id = $this->input->post('adminID');
            $data = array(
                            'username'=> $this->input->post('username'),
                            'password'=> $this->input->post('password'),
                            'date_added'=> $this->input->post('date_added')
                        );
            $this->db->where('adminID', $id);
            $this->db->update('admin', $data);
        }
...?>

Codeigniter/View ...

<?php echo form_open('superuser/update_superuser', array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')); ?>
            <div class="panel panel-default">
              <div class="panel-heading">
                <div class="panel-btns">
                  <a href="#" class="panel-close">&times;</a>
                  <a href="#" class="minimize">&minus;</a>
                </div>
                <h4 class="panel-title">Admin Details</h4>
                <p>Please, Insert your details here below... (for Superuser use only)</p>
              </div>
              <div class="panel-body panel-body-nopadding">

                <!--username-->
                <div class="form-group">
                   <!-- <input type="hidden" name="adminID" class="form-control" value="<?php echo $array->adminID;?>"/> -->
                  <label class="col-sm-4 control-label">Username</label>
                  <div class="col-sm-8">
                    <input type="text" name="username" class="form-control" value="<?php echo $array['username'];?>"/>
                  </div>
                  // <?php // form_error('username');?>
                </div>
                <!--password-->
                <div class="form-group">
                  <label class="col-sm-4 control-label">Password</label>
                  <div class="col-sm-8">
                    <input type="password" name="password" class="form-control" value="<?php echo $array['password'];?>"/>
                  </div>
                  <?php //echo form_error('date_added');?>
                </div>
                <!--Date Added-->
                <div class="form-group">
                  <label class="col-sm-4 control-label">Date</label>
                  <div class="col-sm-8">
                    <input type="text" name="date_added" class="form-control" id="datepicker" value="<?php echo $array['date_added'];?>" />&nbsp;<img src="<?php echo base_url();?>components/backend/images/calendar.gif" alt=""  /><br /><br />
                  </div>
                  <?php //echo form_error('date_added');?>
                </div>

                </div><!-- panel-body -->
              <div class="panel-footer">
                <button class="btn btn-primary">Submit</button>
                <button type="reset" class="btn btn-default">Reset</button>
              </div><!-- panel-footer -->
            </div><!-- panel-default -->
          <?php form_close();?>
...</body></html>

Errors Displayed

A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: admin/edit_admin.php

Line Number: 54
A PHP Error was encountered

Severity: Notice

Message: Undefined index: password

Filename: admin/edit_admin.php

Line Number: 62
A PHP Error was encountered

Severity: Notice

Message: Undefined index: date_added

Filename: admin/edit_admin.php

Line Number: 70

Seems like not correct $array['username'] , $array['password'] and $array['date_added'] . Print out $array first and you'll see what's wrong.

I see a few things... Firstly, you are trying to use the $this->input->post method which is beyond the scope of the model (relevant only to the controller). This is what provides you with the php errors...

Secondly, you are passing the right parameters to the model (minus cleaning them from XSS attacks, notice), but you are accepting different ones, so in the model your function should look something like this

function update_superuser($username, $password, $data_added){

        $id = $this->input->post('adminID'); // I believe you should be getting the id from the database/session honestly, much safer in these cases
        $data = array(
                        'username'=> $username,
                        'password'=> $password,
                        'date_added'=> $data_added
                    );
        $this->db->where('adminID', $id);
        $this->db->update('admin', $data);
    }

Hope this helps!

You are passing 3 parameters to your update_superuser() function here

$this->Superuser_Model->update_superuser($username,$password,$date_added);

and your function in your model only takes 2 params:

function update_superuser($data, $id){ $this->uri->segment(3); $id = $this->input->post('adminID'); $data = array( 'username'=> $this->input->post('username'), 'password'=> $this->input->post('password'), 'date_added'=> $this->input->post('date_added') ); $this->db->where('adminID', $id); $this->db->update('admin', $data); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM