[英]How to insert multiple rows into mysql using Codeigniter Rest Api
I have a form with one add button.我有一个带有一个添加按钮的表单。 Next row will append whenever i click on add button and all entered data should store into database.
每当我单击添加按钮时都会追加下一行,并且所有输入的数据都应存储到数据库中。 I can do for single record but unable to do with multiple records.
我可以做单条记录,但不能做多条记录。 Please provide any help for my issue.
请为我的问题提供任何帮助。 Thanks in advance.
提前致谢。
$this->db->insert_batch() is used for bulk insert. $this->db->insert_batch() 用于批量插入。
Example of how it works (assuming I have a series of 20 records to insert) :它是如何工作的示例(假设我有一系列 20 条记录要插入):
array_push($newRecords, array(
"property1" => 1
"property2" => "two"
));
//...
array_push($newRecords, array(
"property1" => 20
"property2" => "twenty"
));
$this->db->insert_batch("tableName", $newRecords);
There are two different ways to do this:有两种不同的方法可以做到这一点:
you can put insert code in foreach您可以将插入代码放入 foreach
foreach($variable as $key=>$value){
$this->db->insert('table name',array('fieldname'=>'values'));
}
this above method called every time for every loop每次循环都会调用上述方法
or used the batch methods或使用批处理方法
$this->db->insert_batch('table name',$dataArray);
this above method will called one time only.上述方法只会调用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.