简体   繁体   English

使用codeigniter更新数据库记录

[英]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 . 我似乎无法在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 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');
            }

        }
..?>

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 ... Codeigniter /视图 ...

<?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'] . 好像不正确的$array['username']$array['password']$array['date_added'] Print out $array first and you'll see what's wrong. 首先打印$array然后您会发现问题所在。

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->input->post方法,该方法超出了模型的范围(仅与控制器相关)。 This is what provides you with the php errors... 这就是为您提供php错误的原因...

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 其次,您正在向模型传递正确的参数(减去对它们的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);
    }

Hope this helps! 希望这可以帮助!

You are passing 3 parameters to your update_superuser() function here 您将在此处将3个参数传递给您的update_superuser()函数

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

and your function in your model only takes 2 params: 并且模型中的函数仅需要2个参数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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