使用codeigniter将记录插入数据库

时间:2015-12-07 05:57:17

标签: php mysql codeigniter

我是Codeigniter的新手,我想在我的数据库中插入一行。我正在使用codeigniter的表单助手,但我无法将其与我的模型和控制器一起插入。

这是我的控制器:

class Admin extends CI_controller{

        /*add users*/
        public function index () {
            $this->load->view("index");
        }

        public function addstud(){
            $this->load->view("addstud");
        }
        public function addStudRecord(){
            $this->load->library('form_validation');

            $this->form_validation->set_rules('username', 'Username', 'trim|required|min_lenght[10]| callback_check_existing_uname');
            $this->form_validation->set_rules('studpass', 'Password', 'trim|required|min_lenght[10]| max_lenght[32]');
            $this->form_validation->set_rules('studpassCon', 'Password Confirmation', 'trim|required|matches[password]');

            if ($this->form_validation->run() == FALSE){
                $this->load->view("addstud");
            }
            else {
                $this->load->model("membership_model");

                if($query = $this->membership_model->addStudent()){
                    $data['account_created'] = 'Student account has been created!   ';

                    $this->load->view('addstud', $data);
                }
                else {
                    $this->load->view('addstud');
                }
            }

        }

        public function check_existing_uname($reqname) {
            $this->load->model('membership_model');

                $uname_avail = $this->membership_model->check_uname($username);
                if($uname_avail){
                    return TRUE;
                }else{
                    return FALSE;
                } 
        }
  }

和我的模特:

class Membership_model extends CI_Controller {

    public function __construct()
     {
       parent::__construct();
     }

    public function validate () {   //validates account (student)
        $this->db->where('admin_username', $this->input->post('username'));
        $this->db->where("admin_pword", md5($this->input->post("password")));
        $query = $this->db->get("student");

        if($query->num_rows == 1){
            return true;
        }
    }

    public function addStudent(){
        $username = $this->input->post('username');

        $newMember = array(
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
            'fName' => $this->input->post('first-name'),
            'lName' => $this->input->post('last-name'),
            'mName' => $this->input->post('middle-name'),
            'gender' => $this->input->post('gender'),
            'religion' => $this->input->post('religion'),
            'home_add' => $this->input->post('home-add'),
            'telnum' => $this->input->post('telnum'),
            'mobile' => $this->input->post('mobile'),
            'email_address' => $this->input->post('email-add'),
            'lastSchoolAttend' => $this->input->post('lastschool'),
            'lastYear' => $this->input->post('lastyear'),
            'mother' => $this->input->post('mother'),
            'father' => $this->input->post('father'),
            'curAdd' => $this->input->post('curAdd'),
            'lastSchoolAdd' => $this->input->post('name-school'),
            'stud_status' => $this->input->post(`okay`),
            'gName' => $this->input->post('gName'),
            'gOccupation' => $this->input->post('gOccupation'),
            'gCAddress' => $this->input->post('gCAddress'),
            'gContact' => $this->input->post('gContact'),
            'gMobile' => $this->input->post('gMobile'),
            'gRelchild' => $this->input->post('gRelchild')
        );

        $insert = $this->db->insert('student',$newMember);
        return $insert;
    }
 }

在我的视图中:我使用表单控制器而不是html

<?php echo form_open('/admin/addStudRecord', array('class'=>'form', 'role'=>'form', 'method'=>'post')); ?>



<div class="form-group">
                                                    <i class="fa fa-user"></i> <?php echo form_label('First Name'); ?> <?php echo form_error('dname'); ?><!--<label for="first-name">First Name</label>-->
                                                                                              <?php echo form_input(array('type' => 'text', 'name' => 'first-name', 'class' => 'form-control', 'id' => 'first-name'));?>
                                                      <!--<input type="text" name="first-name" placeholder="" class="form-control" value="">-->
                                                    </div>

         /*SOME ATTRIBUTES HERE*/

<?php echo form_submit(array('id' => 'submit', 'value' => 'Add Student', 'type' => 'submit', 'class' => 'btn btn-primary')); ?>
                                <!--<button type="submit" class="btn btn-primary" value=" ">Add Student</button>-->
                                <?php echo form_close();?>
                     </div>

这是正确的方法吗?还是有其他办法吗?

2 个答案:

答案 0 :(得分:1)

在Controller类中添加此构造函数:

public function __construct(){
        parent::__construct();
}

在您的模型类中,使用CI_Model扩展而不是CI_Controller:

class Membership_model extends CI_Model {

答案 1 :(得分:0)

嗯,是的!验证提交的值后,最好将它们存储在数组中并将它们传递给模型:

$this->membership_model->addStudent(); // This function needs to be provided with the inserting data

类似的东西:

$studentData = array(
    'fieldName' => $this->input->post('value')
);
$this->membership_model->addStudent($studentData);