-1

Let say I have my code like this:

$data = array(...);
foreach($data as $val){
    echo $val;
}

So, when I click submit, I want insert all $val to database.

How can I coding it ?

Please help !!

Thank you !!

alessandrio
  • 4,282
  • 2
  • 29
  • 40

1 Answers1

0

First of all you should stop using mysql_*. MySQL supports multiple inserting like

INSERT INTO example
VALUES
 (100, 'Name 1', 'Value 1', 'Other 1'),
 (101, 'Name 2', 'Value 2', 'Other 2'),
 (102, 'Name 3', 'Value 3', 'Other 3'),
 (103, 'Name 4', 'Value 4', 'Other 4');

You just have to build one string in your foreach loop which looks like that

$values = "(100, 'Name 1', 'Value 1', 'Other 1'), (100, 'Name 1', 'Value 1', 'Other 1'), (100, 'Name 1', 'Value 1', 'Other 1')";

and then insert it after the loop

 $sql = "INSERT INTO email_list (R_ID, EMAIL, NAME) VALUES ".$values;

please check this link How to insert array of data into mysql using php

Arun
  • 1,609
  • 1
  • 15
  • 18