【问题标题】:How to use insert_batch to insert data in db如何使用 insert_batch 在 db 中插入数据
【发布时间】:2015-10-18 13:47:18
【问题描述】:

如何使用 insert_batch 通过 for 循环在数据库中添加多个数据,请帮助我,因为我对 codeigniter 非常陌生。

我的控制器

class Student extends CI_Controller {

public function _construct()
{
    parent::_construct();
    //call model
    $this->load->model("StudentModel","m");
}

function index()
{
    $this->load->view("index");
}
function savedata()
{                  
    $data = array(
             array(
  'studentname' => 'Reddy' ,
  'gender' => 'Male' ,
  'phone' => '456879'
  ),

 array(
      'studentname' => 'Yalla' ,
      'gender' => 'Female' ,
      'phone' => '12345678'
       )
    );
//mean that insert into database table name tblstudent

    $this->db->insert_batch('tblstudent',$data);

//mean that when insert already it will go to page index    
    redirect("Student/index");
}

function edit($id)
{
    $row=$this->m->getonerow($id);
    $data['r']=$row;
    $this->load->view('edit',$data);

}


function update($id)
    {
        $id=$this->input->post('id');
        $data=array(
                      'studentname' => $this->input->post('studentname'),
                      'gender'  =>  $this->input->post('gender'),
                      'phone'  =>  $this->input->post('phone')
                   );
                     $this->db->where('id',$id);
                     $this->db->update('tblstudent',$data);
                     redirect("Student/index");

    }
    function delete($id)
    {
        $id=$this->db->where('id',$id);
        $this->db->delete('tblstudent');
        redirect("Student/index");
    }

}

型号

class StudentModel extends CI_Model{

function _construct()
{
    parent::_construct();
}
function gettable()
{
    $query=$this->db->get('tblstudent');
    return $query->result();
}
function getonerow($id)
{
    $this->db->where('id',$id);
    $query = $this->db->get('tblstudent');
    return $query->row();

  }
}

【问题讨论】:

  • 你的多值html代码是什么?

标签: php html codeigniter


【解决方案1】:

首先,您不必添加直接访问控制器中的数据库。对于所有数据库访问代码,您必须创建 model。 在上面的代码中,您在直接控制器中添加了数据库访问代码,根据 MVC 架构,这在逻辑上是错误的。 下面是根据 MVC 的代码,所有数据库访问代码都驻留在模型中。 您批量插入的主要问题是function insert()

模型。请通过它。

<?php

class Student extends CI_Controller {

public function _construct()
{
    parent::_construct();
    //call model
    $this->load->model("StudentModel","m");
}

function index()
{
    $this->load->view("index");
}
function savedata()
{                  

    $stdarray = array();
    $stdarray[] = array('studentname' => 'Reddy' ,'gender' => 'Male' ,'phone' => '456879');
    $stdarray[] = array('studentname' => 'Yalla' ,'gender' => 'Female' ,'phone' => '12345678');
    //mean that insert into database table name tblstudent

    $this->db->insert_batch('tblstudent',$data);
    $this->m->insert($stdarray);

//mean that when insert already it will go to page index    
    redirect("Student/index");
}

function edit($id)
{
    $row=$this->m->getonerow($id);
    $data['r']=$row;
    $this->load->view('edit',$data);

}


function update($id)
    {
        $updateArray = array();
        $updateArray['studentname'] = $this->input->post('studentname');
        $updateArray['gender'] = $this->input->post('gender');
        $updateArray['phone'] = $this->input->post('phone');
        $whereArray = array();
        $whereArray['id'] = $id;
       $this->m->updateRow($updateArray,$whereArray);
       redirect("Student/index");

    }
    function delete($id)
    {
        $whereArray = array();
        $whereArray['id'] = $id;
        $this->m->deleteRow($whereArray);
        redirect("Student/index");
    }

}
?>

型号:

class StudentModel extends CI_Model{

function _construct()
{
    parent::_construct();
}
function gettable()
{
    $query=$this->db->get('tblstudent');
    return $query->result();
}
function getonerow($id)
{
    $this->db->where('id',$id);
    $query = $this->db->get('tblstudent');
    return $query->row();

}
function insert($insrtArray)
{
    $success = $this->db->insert_batch('tblstudent', $insrtArray); 
}
function updateRow($updateArray,$whereArray)
{
    if(!empty($whereArray))
    {
        foreach ($whereArray as $key => $value) {
            $this->db->where($key,$value);
        }
    }       
        $this->db->update('tblstudent',$updateArray);
}
function deleteRow($whereArray)
{
    if(!empty($whereArray))
    {
        foreach ($whereArray as $key => $value) {
            $this->db->where($key,$value);
        }
    }
     $this->db->delete('tblstudent');
}
}
?>

重要提示:此外,您必须对所有直接发送到数据库的输入使用转义函数,以维护 SQL 注入的安全性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多