Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用insert_batch在数据库中插入数据_Php_Html_Codeigniter - Fatal编程技术网

Php 如何使用insert_batch在数据库中插入数据

Php 如何使用insert_batch在数据库中插入数据,php,html,codeigniter,Php,Html,Codeigniter,如何使用insert_batch通过for loop在数据库中添加多个数据请帮助我,因为我对codeigniter非常陌生 我的控制器 class Student extends CI_Controller { public function _construct() { parent::_construct(); //call model $this->load->model("StudentModel","m"); } function index()

如何使用insert_batch通过for loop在数据库中添加多个数据请帮助我,因为我对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");
    }

}
模型


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

当然可以,请检查一下

<?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");
    }

}
?>

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

对于多值,您的html代码是什么?
<?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');
}
}
?>