问题
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);
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.
回答1:
Based on this part of your question:
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 seems to be a problem of database design more so than an error in your code. If your table has a column called Shop Name and you are inserting data into it, you will repeat it fifteen times.
I would suggest breaking up the table into to and linking via a join between them like this:
Table Shops:
ID
Name
Table Products:
ID
shopID // This links to the first table on shopID=ID
... and so on for your *product* information
回答2:
instead of manipulating $_POST in controller manipulate it in model like this
Your controller
$this->load->model('buy_product_model');
$this->buy_product_model->add_product($this->input->post());
EDIT
Make sure the form fields in html form are in array form like this
<input type="text" name="code[]" />
Your model
public function buy_product_model($postdata){
extract($postdata);
$count = count($code);
for($i=0; $i<$count; $i++) {
$data = array(
'shop'=>$shop,
'code' => $code[$i],
'rate' => $rate[$i],
'quantity' => $quantity[$i],
'total' =>($rate[$i]*$quantity[$i])
);
$this->db->insert('[YOUR TABLE]', $data);
}
}
MAKE SURE TO CHANGE [YOUR TABLE] TO YOURS
add rest of the code you have in method add_product
of your model and iterate through a loop as you have done in your controller.
Edit:
Please check 'shop'=>$shop->$this->input->post('shop')
part in your loop
回答3:
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;?>
here is my controller
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();
}
model//
$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;
来源:https://stackoverflow.com/questions/12211542/inserting-multiple-rows-in-database-on-codeigniter