问题
I just want to insert dynamic generated input field data into database . My db table having three fields , id(Auto Increment), product_name and rate . I'm trying to insert bulk data into database using dynamically generated input fields where I can add/remove input fields manually. I created the input fields as
<input class="form-control" placeholder="Product Name" name="prodname[]" type="text">
<input class="form-control" placeholder="Product Rate" name="prodrate[]" type="text">
This is my controller below
function Act_AddProducts() {
if ( $this->input->post( 'prodname' )&&$this->input->post( 'prodrate' )) {
foreach ( $this->input->post( 'prodname' ) as $key => $value ) {
$this->ProductModel->add_products( $value );
}
}
Model function is below
function add_products($val)
{
if($this->db->insert('tbl_product_master', array('product_name' => $val)))
{
return true;
}
else
{
return false;
}
}
Now the value is inserting into db but one at a time. So please help me to identify the issue with code. Also I don't really understand how to insert prodrate[] value into the same insert query.
回答1:
Hope this will help you
Your controller Act_AddProducts
should be like this :
function Act_AddProducts()
{
$prodnames = $this->input->post( 'prodname' );
$prodrates = $this->input->post( 'rate' );
if ( ! empty($prodnames) && ! empty($prodrates) )
{
foreach ($prodnames as $key => $value )
{
$data['product_name'] = $value;
/* make sure product_rate columns is correct i just guess it*/
$data['product_rate'] = $prodrates[$key];
$this->ProductModel->add_products($data);
}
}
}
Your model add_products
should be like this :
function add_products($data)
{
if ( ! empty($data))
{
$this->db->insert('tbl_product_master', $data);
}
}
回答2:
just pass input value to the model as it is, then use foreach inside model
function add_products($val)
{
foreach ( $val as $key => $value ) {
$this->db->insert('tbl_product_master', array('product_name' => $value );
}
}
回答3:
TRY THIS
controller
function Act_AddProducts() {
$product_rate = $data = array();
$product_rate = $this->input->post( 'prodrate' );
$product_name = $this->input->post( 'prodname' )
if ( !empty($this->input->post( 'prodname' ))&&!empty($this->input->post( 'prodrate' ))) {
foreach ( $product_name as $key => $value ) {
$data['product_name'] = $value;
$data['product_rate'] = $product_rate[$key];
$this->ProductModel->add_products($data);
}
}
model
function add_products($data)
{
$product_name = $data['product_name'];
$product_rate = $data['product_rate'];
if($this->db->insert('tbl_product_master', array('product_name' => $product_name,'product_rate' => $product_rate)))
{
return true;
}
else
{
return false;
}
}
回答4:
This is just for your reference.... A simple sample code for dynamic insert.
defined('BASEPATH') OR exit('No direct script access allowed');
class Checking extends CI_Controller {
public function index()
{
echo "<form method='post' action='". base_url("Checking/save") ."'>";
for($i=0;$i<=5;$i++)
{
echo "<input type='text' name='input_text[]'>";
}
echo "<button type='submit'>Submit</button></form>";
}
public function save(){
foreach($this->input->post("input_text") as $Row){
$this->db->insert("checking",array("input_text"=>$Row['input_text']));
}
}
}
create a controller as Checking.php, and run this .. you will get idea
For database
CREATE TABLE `checking` (
`ch` int(11) NOT NULL AUTO_INCREMENT,
`input_text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ch`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
回答5:
If you want upload bulk record then use insert_batch instead of simple insert your Controller should be
function Act_AddProducts()
{
$product_rate = $_POST['prodrate'];
$product_name = $_POST['prodname'];
if(!empty($product_rate) && !empty($product_rate)){
$data_array = array();
foreach ($product_rate as $key => $value )
{
$tmp_array = array();
$tmp_array['product_name'] = $value;
$tmp_array['product_rate'] = $product_rate[$key];
$data_array[] = $tmp_array;
}
$this->ProductModel->add_products($data_array);
}
model should be
function add_products($data)
{
if($this->db->insert_batch('tbl_product_master', $data))
{
return true;
}
else
{
return false;
}
}
来源:https://stackoverflow.com/questions/51319095/how-to-insert-dynamic-data-in-codeigniter