使用codeigniter更新数据库记录

时间:2015-03-28 15:05:59

标签: php codeigniter model-view-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');
            }

        }
..?>

笨/型号

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

3 个答案:

答案 0 :(得分:0)

似乎不正确$array['username']$array['password']$array['date_added']。首先打印$array,然后您就会看到错误。

答案 1 :(得分:0)

我看到了一些事情......首先,您正在尝试使用超出模型范围的$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);
    }

希望这有帮助!

答案 2 :(得分:0)

您将3个参数传递给update_superuser()函数

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

并且你的模型中的函数只需要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); }