gpt4 book ai didi

php - 如何使用insert_batch在db中插入数据

转载 作者:行者123 更新时间:2023-11-28 03:08:11 25 4
gpt4 key购买 nike

如何使用 insert_batch 通过 for 循环在数据库中添加多个数据 请帮助我,因为我是 codeigniter 的新手。

我的 Controller

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

}
}

最佳答案

首先,您不必在 Controller 中添加对数据库的直接访问。对于所有数据库访问代码,您必须创建模型。 在上面的代码中,您在直接 Controller 中添加了数据库访问代码,根据 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 注入(inject)的安全性。

关于php - 如何使用insert_batch在db中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com