点击提交时,Codeigniter无法更新表单

时间:2017-11-30 17:47:21

标签: php jquery html css codeigniter

我在使用codeigniter时遇到问题,当我点击提交时,它不会更新数据。

我正在创建超级管理员,以便删除或更新正常的管理员数据。

这是我的代码:

控制器

<?php

class Superadmin extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        //$this->session->userdata('is_login') == FALSE || redirect('admin/Login_admin');
        if($this->session->userdata('token'))
        $this->session->unset_userdata('token');
        $this->load->model('admin/Admin_m');
    }

    public function index()
    {
        if(isset($this->session->id)=='') 
        {
            redirect('admin/Login');
        }

        $data['content'] = 'page/admin/superadmin';
        $data['admin'] = $this->Admin_m->get_all();
        $this->load->view('layout_admin', $data);
    }

    public function editadmin($admin_id)
    {
        if(isset($this->session->id)=='') 
        {
            redirect('admin/Login');
        }

        $data['content'] = 'page/admin/superadminedit';
        $data['admin']   = $this->Admin_m->get_admin_by_id($admin_id);

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('page/admin/superadminedit', $data);
        }
        else
        {
        $this->form_validation->set_rules('full_name', ' Full name', 'required');
        $this->form_validation->set_rules('user', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
            // kondisi untuk input data jika password kosong
            if($this->input->post('password') == ''){
                $data = 
                array(
                'full_name' => $this->input->post('full_name'),
                'user'      => $this->input->post('user'),
                'level'     => $this->input->post('level'),
                );
            }
            else
            {
            $data = 
                array(
                'full_name' => $this->input->post('full_name'),
                'user'      => $this->input->post('user'),
                'password'  => hashpassword($this->input->post('password')),
                'level'     => $this->input->post('level'),
                );
            }
                $this->Admin_m->edit_admin($admin_id,$data);
                redirect('admin/superadmin/editadmin');
        } 
    }


    public function deleteadmin($id)
    {
        if(isset($this->session->id)=='') 
        {
            redirect('admin/Login');
        }

        $this->load->model('Admin_m');

        $this->Admin_m->delete($id);

        //$this->session->set_flashdata('remove_success', 'Admin berhasil dihapus dari List');

        redirect('admin/Superadmin');
    }

}

?>

模型

<?php

class Admin_m extends CI_Model
{
    public function check_akunadmin($user= '' , $password = '')
    {
        return $this->db->get_where('admin_user', 
            array('user' =>$user, 
                  'password' => hashpassword($password)
                  )
                    )->row();

    }

    public function insert($data = array())
    {
        $this->db->insert('admin_user',$data);
    }

    public function edit_admin($id, $data = array())
    {
         $this->db->where('id', $id);
         $this->db->set($data);
         $this->db->update('admin_user');
    }

    public function get_admin_by_id($admin_id)
    {
        return $this->db->get_where('admin_user', array('id' => $admin_id))->row();
    }

    public function get_all($per_page = 0, $offset = 0)
    {
        $this->db->order_by('id', 'desc');
        return $this->db->limit($per_page, $offset)->get('admin_user')->result();
    }

    public function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('admin_user'); 
    }

}

?>

查看

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>AdminLTE 2 | General Form Elements</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="<?php echo base_url();?>backendtampilan/bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="<?php echo base_url();?>backendtampilan/dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="<?php echo base_url();?>backendtampilan/dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <header class="main-header">
    <!-- Logo -->
    <a class="logo">
      <!-- mini logo for sidebar mini 50x50 pixels -->
      <span class="logo-mini"><b>A</b>LT</span>
      <!-- logo for regular state and mobile devices -->
      <span class="logo-lg"><b>Admin</b>LTE</span>
    </a>
    <!-- Header Navbar: style can be found in header.less -->
  </header>
  <!-- Left side column. contains the logo and sidebar -->

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Edit Admin
      </h1>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <!-- left column -->
        <div class="col-md-12">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Super Admin edit Page</h3> <br>
              <a href="<?php echo site_url('admin/superadmin')?>"><button class="btn btn-primary">Back to Super Admin</button></a>
               <?php if(validation_errors()): ?>
                <ul class="alert alert-danger"><?php echo validation_errors('<li>','</li>')?></ul>
             <?php endif;?>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
                <form action="<?php echo base_url(). 'admin/superadmin/editadmin/'. $admin->id; ?>" method="post" enctype ="multipart/form-data">
              <div class="box-body">
                <div class="form-group">
                  <label>ID</label>
                  <input type="text" class="form-control" placeholder="ID" value="<?php echo $admin->id ?>" disabled>
                </div>

                <div class="form-group">
                  <label >Full Name</label>
                  <input type="text" class="form-control" name="full_name"  placeholder="Enter Name" value="<?php echo $admin->full_name ?>">
                </div>

                 <div class="form-group">
                  <label>User</label>
                  <input type="text" class="form-control" name="user" value="<?php echo $admin->user ?>">
                </div>

              <div class="form-group">
                  <label>New Password</label>
                  <input type="text" class="form-control" name="password" placeholder="password" >
              </div>

              <div class="form-group">
                <label>Level</label><br>
                <select name="level">
                  <option value="0">Super Admin</option>
                  <option value="1">Normal Admin</option>
                </select>
              </div>

              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
          <!-- /.box -->
        <!--/.col (right) -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  <footer class="main-footer">
    <div class="pull-right hidden-xs">
    </div>
     All rights reserved.
  </footer>


<!-- ./wrapper -->

<!-- jQuery 2.2.3 -->
<script src="<?php echo base_url();?>backendtampilan/plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="<?php echo base_url();?>backendtampilan/bootstrap/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="<?php echo base_url();?>backendtampilan/plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="<?php echo base_url();?>backendtampilan/dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="<?php echo base_url();?>backendtampilan/dist/js/demo.js"></script>
</body>
</html>

点击视图页面上的提交后,它不会在数据库中更新...

出了什么问题?我希望有人理解并向我解释......

非常感谢你帮助我。

1 个答案:

答案 0 :(得分:1)

试试这个:

public function edit_admin($id, $data = array())
{
    $this->db->where('id', $id);
    $this->db->update('admin_user', $data);
}

另外,执行print_r($ data);退出;就在检查模型是否正确接收数据之前。

OFF:让我帮你一把......没有必要重复整个阵列。

if($this->input->post('password') == ''){
    $data = 
    array(
        'full_name' => $this->input->post('full_name'),
        'user'      => $this->input->post('user'),
        'level'     => $this->input->post('level'),
    );
}
else
{
    $data = 
    array(
        'full_name' => $this->input->post('full_name'),
        'user'      => $this->input->post('user'),
        'password'  => hashpassword($this->input->post('password')),
        'level'     => $this->input->post('level'),
    );
}

应该是:

$data = 
array(
    'full_name' => $this->input->post('full_name'),
    'user'      => $this->input->post('user'),
    'level'     => $this->input->post('level'),
);

if(!empty($this->input->post('password'))){
    $data['password'] = hashpassword($this->input->post('password'));
}