簡體   English   中英

使用codeigniter向數據庫插入一條記錄

[英]Inserting a record to the database with 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"/>
    <?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>

這是正確的方法嗎? 或者有另一種方法嗎?

在您的控制器類中添加此構造函數:

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

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

class Membership_model extends CI_Model {

嗯,是! 驗證提交的值后,最好將它們存儲在數組中並將它們傳遞給模型:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM