使用CodeIgniter添加用户

时间:2016-01-19 05:58:41

标签: database codeigniter add

首先,我是新来的,我真的不知道如何使用codeigniter,现在我很困惑。所以我目前正在尝试使用codeigniter向数据库添加数据。当我单击提交按钮时,没有任何内容可以显示,然后我检查了我的数据库并且没有添加任何记录。你们能帮助我吗?

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class M_users extends CI_Controller {

        public function index()
        {
             if ($this->session->userdata('is_logged_in')){
              $this->load->helper('breadcrumb_helper');
            $this->load->view('imports/head');
            $this->load->view('header');
            $this->load->view('musers');
            $this->load->view('imports/footer');
            } else {
                redirect('home/restricted');
            }  
        }


        /* display users */
        public function getuser(){
            $this->load->model('model_emp');
            $this->data['emp'] = $this->model_emp->get_allus();
            $this->load->view('getuser',$this->data); 
        }


         /* add users */
        public function adduser(){
            $this->load->library('form_validation');
            $this->load->helper('security');

           $this->form_validation->set_rules('username','Username','required|min_length[4]|trim|callback_users_exist|xss_clean');
           $this->form_validation->set_rules('password','Password','trim|required|min_length[6]|max_length[50]|matches[password_conf]|md5|trim|xss_clean');
           $this->form_validation->set_rules('password_conf','Confirmed Password','trim|required|min_length[6]|max_length[50]|matches[password_conf]|md5|trim|xss_clean');
           $this->form_validation->set_rules('firstname','Firstname','trim|required|xss_clean');
           $this->form_validation->set_rules('middlename','Middlename','trim|required|xss_clean');
           $this->form_validation->set_rules('lastname','Lastname','trim|required|xss_clean');
           $this->form_validation->set_rules('screenname','Screenname','trim|required|xss_clean');
           $this->form_validation->set_rules('gender','Gender','trim|required');
           $this->form_validation->set_rules('position','Position','trim|required');

           if($this->form_validation->run()){

               $this->load->model('model_emp');
              $result=$this->model_emp->addusers();

               if(!$result){
                   echo mysqli_error($result);
               }else{
                   echo "<div class='alert alert-success alert-dismissable'>
                   <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>Successfully Added</div>";
               }
           }
            echo validation_errors("<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>","</div>");
        }

        /*edit assets */


  /* users exists */  
        public function users_exists()
        {
             $this->load->model('model_emp');
            if ($this->model_emp->users_exists())
            {
                return true;
            } else
             {
                 $this->form_validation->set_message('users_exists','User Already Existed.');
                 return false;
            }
          }
}

型号:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_emp extends CI_Model{

     function __construct() {
        $this->postTable = 'posts';
    }

public function get_allus(){

            $this->db->select('*');
            $this->db->from('employee'); 
            $query = $this->db->get(); 
            if($query->num_rows() != 0)
            {   
                return $query->result_array();
            }
            else
            {
                return false;
            }

    }


     public function addusers(){
     $array=array('Username'=>$_POST['Username'],'email'=>$_POST['email'],'password'=>$_POST['Password'],'emp_name'=>$_POST['emp_name'],
                  ,'position'=>$_POST['Position'],'emp_pic'=>$_POST['emp_pic'] );
     $this->db->set($array);
     $query=$this->db->insert('employee');
      return $query;
    }

    public function users_exists(){
        $this->db->where('Username', $this->input->post('Username'));
        $query = $this->db->get('employee');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }

查看:

     <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            <i class="fa fa-users fa-fw"></i>Manage Users

          </h1>


            <?php
           echo create_breadcrumb();
            ?>


        </section>

<!-- top -->
<section class="content">
<div class="box box-info">
                <div class="box-header with-border">



                            <hr>


        <h2 class="sub-header">Employees</h2>
                    <div class="pull-right box-tools">
                        <button class="btn btn-default btn-sm" data-toggle="modal" data-target=".bs-addusers-modal-lg">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                        </button>
                        <button class="btn btn-default btn-sm" data-toggle="modal" data-target=".bs-editusers-modal-lg">
                        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                        </button>
                        <button class="btn btn-default btn-sm" data-toggle="modal" data-target=".bs-deleteusers-modal-lg">
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                        </button>
                    </div>






</section>              
<?php $this->load->view('modals/addusers');  ?>
</div>

模态:

<div id="add_usersodal"class="modal fade bs-addusers-modal-lg">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                   <h3 class="modal-title">Add User</h3>
                   <hr>
                  </div>
                  <div class="modal-body">
                  <!-- result message -->
                  <center><div style="font-size:10px;width:50%;" id="info"></div></center>

                         <div class="form-group">
                            <div class="col-md-15">
                   <!--user form -->
                    <div id="adduser" class="add_user_group">           
                        <form id="addusers" method="post" action="">
                            <div class="form-group">




                    <div class="form-group">
                    <input   type="text"  class="form-control" placeholder="Username*" name="Username" required/>
                    </div>
                    <div class="form-group">
                    <input   type="text" class="form-control" placeholder="Email*" name="Email" required/>
                    </div>
                    <div class="form-group">
                    <input   type="password" class="form-control" placeholder="Password*" name="Password" required/>
                    </div>
                    <div class="form-group">
                    <input   type="password" class="form-control" placeholder="Confirmed Password*" name="password_conf" required/>
                    </div>
                    <div class="form-group">
                    <input   type="text" class="form-control" placeholder="First Name*" name="Firstname" required/>
                    </div>
                    <div class="form-group">
                    <input   type="text" class="form-control" placeholder="Middle Name*" name="Middlename" required/>
                    </div>
                    <div class="form-group">
                    <input   type="text" class="form-control" placeholder="Last Name*" name="Lastname" required>
                    </div>
                    <div class="form-group">
                    <input   type="text" class="form-control" placeholder="Screen Name" name="Screenname">
                    </div>
                    <div class="form-group">
                      <label>Gender*</label>
                      <select name="Gender" required>
                      <option value="">Select..</option>
                      <option value="M">Male</option>
                      <option value="F">Female</option>
                    </select>


                      <label>Position*</label>
                      <select name="Position" id="sel_user" required>
                      <option value="">Select..</option>
                      <option value="option1">CITO</option>
                      <option value="option2">IT Personnel</option>
                      <option value="option3">Regular</option>
                      </select>
                    </div>
                     </div>
                      </div>
                     <div class="col-md-12" style="margin-top:2%">
                            <button class="btn btn-primary">Submit</button>
                            </div>

                          </div>
                        </form>

                        </div>

                        </div>

                  <div class="modal-footer">
                    <button type="button" data-dismiss="modal">Close</button>
                  </div>
                </div><!-- /.modal-content -->
              </div><!-- /.modal-dialog -->
            </div>

3 个答案:

答案 0 :(得分:0)

以下是您可以从中获得想法的示例代码,控制器控制您的应用程序逻辑模型执行与数据库相关的操作,并查看您的网页。

控制器:user.php

function registration()
{
  $this->load->library('form_validation');
        $this->form_validation->set_rules(array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required'
            )
        ));

        if(!$this->form_validation->run()) 
        {
            $this->load->view('registration_view');
        } 
        else 
        {
            $this->load->model('user_model');
            $user = new users_model();
            $user->username = $this->input->post('username');
            $user->password = $this->input->post('password');
            $user->save();
        }
}

查看:registraion_form.php

<form action="<?php echo site_url('user/registration'); ?>" method="post">
 <input type="text" name="username" value="<?php echo set_value('username'); ?>" />
 <input type="password" name="password" value="<?php echo set_value('password'); ?>" />
 <input type="submit" name="submit" value="submit"/>
</form>

型号:user_model.php

public $username;
public $password;

public function save()
{
   $data = array(
     'username' => $this->username,
     'password' => $this->password
   );
   $this->db->insert('table-name', $data);
}

答案 1 :(得分:0)

如果我正确,那会导致你的控制器应该包含视图中的post值。

$username = $this->input->post('Username')

您应该包含所有字段,以便表单验证工作,否则会抛出错误(可能是空白页)。

所以,而不是你对你的模型所做的。它应该看起来像这样,

public function addusers($username, $email, $password, $emp_name, $position, $emp_pic){
   $data = array(
    'username' => $username ,
   'email' => $email ,
   'password' => $password,
    'emp_name' => $emp_name,
    'position' => $position,
    'emp_pic' => path_to_your_employee_pic
  );

$this->db->insert('employee', $data);
}

使用从控制器传递的参数

$this->model_emp->addusers($username, $email, $password, $emp_name, $position, $emp_pic);

答案 2 :(得分:0)

对控制器中的adduser()进行一些更改

&#13;
&#13;
public function adduser(){
  $this->load->library('form_validation');
  $this->load->helper('security');
  $this->form_validation->set_rules('username','Username','required|min_length[4]|trim|callback_users_exist|xss_clean');
           $this->form_validation->set_rules('password','Password','trim|required|min_length[6]|max_length[50]|matches[password_conf]|md5|trim|xss_clean');
           $this->form_validation->set_rules('password_conf','Confirmed Password','trim|required|min_length[6]|max_length[50]|matches[password_conf]|md5|trim|xss_clean');
           $this->form_validation->set_rules('firstname','Firstname','trim|required|xss_clean');
           $this->form_validation->set_rules('middlename','Middlename','trim|required|xss_clean');
           $this->form_validation->set_rules('lastname','Lastname','trim|required|xss_clean');
           $this->form_validation->set_rules('screenname','Screenname','trim|required|xss_clean');
           $this->form_validation->set_rules('gender','Gender','trim|required');
           $this->form_validation->set_rules('position','Position','trim|required');

if($this->form_validation->run()){
  $this->load->model('model_emp');
  $data = array(
                 'username'    =>$this->input->post('Username'),
                 'password'    =>$this->input->post('Password'),
                 'firstname'   =>$this->input->post('Firstname'),
                 'middlename'  =>$this->input->post('Middlename'),
                 'lastname'    =>$this->input->post('Lastname'),
                 'screenname'  =>$this->input->post('Screenname'),
                 'gender'      =>$this->input->post('Gender'),
                 'position'    =>$this->input->post('Position'),
               );

  $result=$this->model_emp->addusers($data);
  if(!$result){
                   echo mysqli_error($result);
               }else{
                   echo "<div class='alert alert-success alert-dismissable'>
                   <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>Successfully Added</div>";
               }
           }
            echo validation_errors("<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>","</div>");
        }
&#13;
&#13;
&#13;

在模型中将函数更改为

&#13;
&#13;
  public function addusers($data=null){    
    if(!empty($data)){
        $this->db->insert('table_name',$data);
        $ins_id = $this->db->insert_id();
        return $ins_id;
    } else {
      return false;
    }
}
&#13;
&#13;
&#13;