使用codeigniter更新数据库记录


Update a database record using codeigniter

我遇到了一个问题。。。这是一个令人沮丧的问题我似乎无法编辑Codeigniter中的特定行。我在这里发现了以前的问题,甚至尝试了解决方案,但都无济于事。我正在为我正在进行的这个项目争取时间在您将此问题视为重复问题之前,请先看一看任何帮助都将不胜感激。。。提前感谢我的代码片段如下:

代码点火器/控制器

<?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');
            }
        }
..?>

代码点火器/型号

    <?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);
        }
...?>

代码点火器/视图

<?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>

显示的错误

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

似乎不正确的$array['username']$array['password']$array['date_added']。先打印出$array,你就会发现哪里出了问题。

我看到了一些东西。。。首先,您正在尝试使用$this->input->post方法,该方法超出了模型的范围(仅与控制器相关)。这就是为您提供php错误的原因。。。

其次,您将正确的参数传递给模型(注意,减去从XSS攻击中清除它们),但您接受的是不同的参数,因此在模型中,您的函数应该类似于以下

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);
    }

希望这能有所帮助!

您在这里向update_superuser()函数传递3个参数

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

并且你的模型中的函数只需要两个参数:

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); }