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">
这是我下面的控制器
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 );
}
}
模型函数如下
function add_products($val)
{
if($this->db->insert('tbl_product_master', array('product_name' => $val)))
{
return true;
}
else
{
return false;
}
}
现在该值正在插入到数据库中,但一次插入一个。所以请帮我找出代码问题。我也不太明白如何插入比例[]值到同一个插入查询中。
希望对你有帮助
你的控制器Act_AddProducts
应该是这样的:
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);
}
}
}
你的模型add_products
应该是这样的:
function add_products($data)
{
if ( ! empty($data))
{
$this->db->insert('tbl_product_master', $data);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)