-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 !!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

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.