0

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.

3
  • 3
    You don't seem to close your for loop anywhere, is that just a bad copy and paste from your code - or should I post that as the answer? Commented Aug 31, 2012 at 8:30
  • i closed it but missed in time of upload Commented Aug 31, 2012 at 8:38
  • Could you pop it in where it is in your code please? Commented Aug 31, 2012 at 8:38

3 Answers 3

0

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
Sign up to request clarification or add additional context in comments.

3 Comments

its for, from which shop i have purchased those product.
@RahatIslamKhan If you need the data, why are you unhappy with it being inserted? I don't understand.
i may purchase many product from a shop.thats why i want to insert all the data at the same time
0

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

2 Comments

it insert only one row and only the shop name nothing else.rest of the rows are not inserted in database
@RahatIslamKhan, check my solutions above, I have edited.
0

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.