Codeigniniter通过模型和控制器插入数据 [英] Codeigniniter insert data through models and controller

查看:60
本文介绍了Codeigniniter通过模型和控制器插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器我下面是 form_ctrl 代码

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

class form_ctrl extends CI_Controller {

    public function index()
    {
        //$this->load->view('welcome_message');
             $this->load->helper(array('form', 'url'));
             $this->load->library('form_validation');

                //$this->form_validation->set_rules('name', 'Username', 'required');
              $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                $this->form_validation->set_rules('pass', 'Password', 'required',
                        array('required' => 'You must provide a %s.')
                );
                $this->form_validation->set_rules('email', 'Email', 'required');
                $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('table');
                }
                else
                {
                        $this->load->view('results');
                        $name=$this->input->post('name');
                        $pass=$this->input->post('pass');
                        $email=$this->input->post('email');
                        $mobile=$this->input->post('mobile');
            $address=$this->input->post('address');
                             $data = array(
                                           'name' =>$name ,
                                           'pass' => $pass,
                                           'email' => $email,
                                           'mobile' => $mobile,
                       'address' => $address
                                           );
                            $this->db->insert('form', $data);

                                       }
        }
}

查看I具有 result.php 代码

 <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <?php echo validation_errors(); ?>
    <?php echo form_open(); ?>
    <table >
    <tr>
        <td colspan=2 align="center"><h3>User Details</h3></td>
    </tr>
    <tr>
        <td>
            <?php echo form_label('Name'); ?>
        </td>
        <td>
            <?php echo form_input(array('id' => 'name', 'name' => 'name')); ?>
        </td>
    </tr>
    <tr>
        <td>
            <?php echo form_label('Pass'); ?>
        </td>
        <td>
            <?php echo form_password(array('id' => 'pass', 'name' => 'pass')); ?>
        </td>
    </tr>
    <tr>
        <td><?php echo form_label('Email'); ?>
    </td>
    <td><?php echo form_input(array('id' => 'email', 'name' => 'email')); ?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Mobile'); ?>
    </td>
        <td><?php echo form_input(array('id' => 'mobile', 'name' => 'mobile')); ?>
    </td>
    </tr>
    <tr>
        <td><?php echo form_label('Address'); ?>
    </td>
        <td><?php echo form_input(array('id' => 'address', 'name' => 'address')); ?>
    </td>
    </tr>
    <tr>
        <td colspan="2" align="center"><?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
    </td>
    </tr>
    <?php echo form_close(); ?>
    </table>
    </body>
    </html>

在此代码中,我想包含用于插入数据而不是控制器的模型。

In this code I want to include model to insert the data instead of controller.

代码可以正常工作以将数据插入数据库,但是我希望通过模型而不是控制器来实现。我尝试了很多次,但没有得到理想的结果。

The code is working properly for insert data into database but I want this through model not from controller. I tried so many times but I didn't get the desirable result.

推荐答案

commonModel.php

class CommonModel extends CI_Model {

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

    public function insert($tableName,$data){

       return $this->db->insert($tableName, $data);

    }
}

像这样替换您的控制器代码

replace your controller code like this

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

class form_ctrl extends CI_Controller {

    public function index()
    {
        //$this->load->view('welcome_message');
             $this->load->helper(array('form', 'url'));
             $this->load->library('form_validation');
             $this->load->model('commonModel');

                //$this->form_validation->set_rules('name', 'Username', 'required');
              $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                $this->form_validation->set_rules('pass', 'Password', 'required',
                        array('required' => 'You must provide a %s.')
                );
                $this->form_validation->set_rules('email', 'Email', 'required');
                $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('table');
                }
                else
                {
                        $this->load->view('results');
                        $name=$this->input->post('name');
                        $pass=$this->input->post('pass');
                        $email=$this->input->post('email');
                        $mobile=$this->input->post('mobile');
            $address=$this->input->post('address');
                             $data = array(
                                           'name' =>$name ,
                                           'pass' => $pass,
                                           'email' => $email,
                                           'mobile' => $mobile,
                       'address' => $address
                                           );
                            $this->commonModel->insert('form', $data);


                                       }
        }
}

这篇关于Codeigniniter通过模型和控制器插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆