在codeigniter上的数据库中插入多行 [英] inserting multiple rows in database on codeigniter
本文介绍了在codeigniter上的数据库中插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Possible Duplicate:
insert multiple rows via a php array into mysql
我试图只插入填充数据到数据库。我的控制器
i am trying to insert only filled data in to the database. my controller
$code=$_POST['code'];
$rate=$_POST['rate'];
$quantity=$_POST['quantity'];
//$total=$_POST['rate']*$_POST['quantity'];
$count = count($_POST['code']);
for($i=0; $i<$count; $i++) {
$data = array(
'shop'=>$shop->$this->input->post('shop'),
'code' => $code[$i],
'rate' => $rate[$i],
'quantity' => $quantity[$i],
'total' =>($rate[$i]*$quantity[$i])
);
$this->load->model('buy_product_model');
$this->buy_product_model->add_product($data);
我有一个下拉列表选择商店和那个商店我创建了15个输入字段。字段在上面的问题是如果我只填充一个或两个值,它创建15行在数据库和15时间重新打造商店名称。任何人都可以解决这个问题。
i have a drop downlist to select shop and for that shop i have created 15 input field.the fields are on the above.the problem is if i only fill up only one or two value it creates 15 rows in the database and 15 time repate the shop name.Can anyone fix this problem.
推荐答案
最后一次修复it.here是我的意见
at last i fix it.here is my view
<?php for($i = 1; $i <=10; $i++):?>
<tr>
<td>
<?php echo $i;?>
</td>
<td>
<input type="text" name="code[]" value="<?php echo '';?>" id="code" />
</td>
<td>
<input type="text" name="rate[]" value="<?php echo '';?>" id="rate" />
</td>
<td>
<input type="text" name="quantity[]" value="<?php echo '';?>" id="quantity" />
</td>
</tr>
<?php endfor;?>
这里是我的控制器
if (empty($_POST))
{
$this->index();
}
else
{
//insert to database
$this->load->model('buy_product_model');
$data= $this->buy_product_model->add_product();
//echo "success";
$this->index();
}
模型//
$data = array();
$todayDate = date('Y-m-d');
for($i = 0; $i < count($_POST['code']); $i++)
{
if($_POST['code'][$i] != '')
{
$data[] = array(
'code' => $_POST['code'][$i],
'shop' => $_POST['shop'],
'rate' => $_POST['rate'][$i],
'quantity' => $_POST['quantity'][$i],
'total' =>( $_POST['rate'][$i]*$_POST['quantity'][$i]),
'date' => $todayDate
);
}
}
$dataCount = count($data);
if($dataCount)
{
$this->db->insert_batch('purchase', $data);
}
return $dataCount;
这篇关于在codeigniter上的数据库中插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文