2

I want to insert multiple rows into a table using for loop, but having some errors. What is wrong in this code?

$sql=mysql_query("INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES

            for ($i=0;$i<$count;$i++) 
            {
                ('$id','$data['data']['name_'.$i]','$data['data']['val_'.$i]')");
            } 
0

6 Answers 6

4

Try it:

$sql = "INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES";
$values = [];

for ($i=0;$i<$count;$i++)
{
    $values[] = "('$id','$data['data']['name_$i]','$data['data']['val_$i]')";
}

$sql .= join(',', $values);
$result = mysql_query($sql);
Sign up to request clarification or add additional context in comments.

3 Comments

then try $values = array();
Doesn't this accomplish him saving all his data in one row instead of multiple rows?
@rokas thanks, i now see it. Didn't not know you could send multiple rows like that
3

Codeigniter active record has a function insert_batch i think that is what you need:

$data = array(
array(
  'p_id' => 'My id' ,
  'po_name' => 'My Name' ,
  'po_val' => 'My val'
),
array(
  'title' => 'Another title' ,
  'name' => 'Another Name' ,
  'date' => 'Another date'
)
);

$this->db->insert_batch('pl_tbl', $data); 

Other way:

for ($i=0;$i<$count;$i++) 
  {
     $data = array(
      array(
      'p_id' => $id ,
      'po_name' => $data['data']['name_'.$i] ,
      'po_val' => $data['data']['val_'.$i]
     );
     $this->db->insert('pl_tbl', $data); 
  }

2 Comments

i got like this INSERT INTO pl_tbl () VALUES (), (), ()
please use insert_batch function form CI
2

try this simple code its should work for you..

<?php
  for($i=0;$i<$count;$i++)
  {
     $sql="INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES ('$id','$data['data']['name_'.$i]','$data['data']['val_'.$i]')";
     $result = mysql_query($sql);
  }
?>

thanks

Comments

1
$data=array();
for ($i=0;$i<$count;$i++) 
{
   $temp=array();
   $temp['p_id'] = 'My id'; //actual value
   $temp['po_name'] = 'My Name'; //actual value
   $temp['po_val'] = 'My val'; //actual value
   array_push($data,$temp);
}
$this->db->insert_batch('pl_tbl', $data);

You can try with this code.

1 Comment

i updated my code. please try again. i hope it will be working.
0

try to save the whole query in a variable and then run with the mysql_query should help you.

so code will be

$sql_query="INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES ";

    for ($i=0;$i<$count;$i++) 
    {
        $sql_query.="('$id','".$data["data"]["name_".$i]."','".$data["data"]["val_".$i]."') ";
    }
$sql=mysql_query($sql_query);

Comments

0

If I understood this correctly, you should be doing this

for ($i=0;$i<$count;$i++) {
    $sql=mysql_query("INSERT INTO pl_tbl (p_id,po_name,po_val) VALUES  ('$id','".$data['data']['name_'.$i]."','".$data['data']['val_'.$i]."')");
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.