du131642 2014-09-17 20:19
浏览 49

Codeigniter数据库插入数据

I'm new to codeigniter and there is a question i want to ask about inserting information in my mysql database.I have a home.php controller, where from i can get data from my database via a model I have made, bud the opposite thing -> inserting data in my db, i can't do it. So, i have a controller function -> insertData, which calls a VIEW -> viewInserts, and in this VIEW i have a from where I want to take the insert data and redirect it to my database. Can you help me about it. I want to make a MODEL which will trigger this action and pass it to the VIEW but am confused right now about the logic. Does the insert_batch() function will do it ?

  • 写回答

1条回答 默认 最新

  • doushi8186 2014-09-19 07:52
    关注
        Very simple
        1. call view from controller 
        2. submit form
        3. save data in db or send to data in model here can add data
    
    1. Controller  test.php
    
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class test extends CI_Controller {
    
        public function __construct()
        {
           parent::__construct();
    
           $this->load->helper(array('form', 'url'));
           $this->load->library('form_validation');
        }
        public function add()       
        {
              $this->load->view('test');
        }
        public function save()
        {
            //write form validation
            $this->form_validation->set_rules('fname','First Name','required|min_length[3]');
            $this->form_validation->set_rules('lname','Last Name','required|min_length[3]');
    
            if ($this->form_validation->run() == FALSE) {
                //error in validation redirect to form
                $this->add();
            }
          else {
    
               $fname = $this->input->post('fname');
               $lname = $this->input->post('lname');
    
               $insertData = array ('fname'=> $fname,
                                    'lname'=>$lname );
    
              //save data using model function or save data directly here
              /*
              $this->load->model('test_model');  
              $this->test_model->insertRecord($insertData);
              */
    
              $this->db->insert('testtbl',$insertData);
              redirect(base_url().'add');     
            }
       }
    }?>
    
    2. view test.php
    
    <form id="testFrm" name="testFrm" action="<?php base_url().?>test/save" method="post">
      <input type="text" id="fname" name="fname" value="<?php set_value('fname');?>" >
      <input type="text" id="lname" name="lname" value="<?php set_value('lname');?>" >
      <input type="submit" id="subBtn" name="subBtn" value="Save" >
    </form>
    
    
    3. Model test_model.php
    
    <?php
    if (!defined('BASEPATH'))exit('No direct script access allowed');
    
    class Test_Model extends CI_Model {
    
        function insertRecord($insertData) {
           $this->db->insert('testtbl',$insertData);
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思