Ramya Ramya - 27 days ago 8
PHP Question

Inserting number of rows and input post usage

I am trying to insert data into a table in a

for
loop:

public function insertprolist()
{
for ($i = 1; $i < $this->input->post('numrows'); $i = $i + 1)
{
$data = array(
'productname' => $this->input->post('proname'+$i),
'quantity' => $this->input->post('quantity'+$i),
'price' => $this->input->post('price'+$i),
'amount' => $this->input->post('amount'+$i)
);
return $this->db->insert('purchaseprolist', $data);
}
}


I am getting an error.

It is not getting inserted into the database table.

I have appended new rows with input fields with names such as
proname1
,
quantity1
and for the second row the input name attribute will be generated as
proname2
,
quantity2
and it goes on.

I am retrieving the number of rows in another input with name
numrows
.

Now I want to access these inputs in modal file.

Answer

In PHP, you can not use + sign for concatenation, you need to use (.) dot here:

$data = array(
        'productname' => $this->input->post('proname'.$i),
        'quantity' => $this->input->post('quantity'.$i),
        'price' => $this->input->post('price'.$i),
        'amount' => $this->input->post('amount'.$i)
        );

Second, you are using return, this will stop your function after ist iteration, you can not use it inside the for loop.

Third, before inserting, try to check your post value what are you getting this will help you understand what are you trying.

print_r($_POST); // before for loop start.

Fourth, instead of multiple queries execution, i suggest you to use batch_insert(), this will help you: http://www.codeigniter.com/userguide3/database/query_builder.html

Comments