[php] How to insert array of data into mysql using php

Currently I have an Array that looks like the following when output thru print_r();

Array
(
    [0] => Array
        (
            [R_ID] => 32
            [email] => [email protected]
            [name] => Bob
        )

    [1] => Array
        (
            [R_ID] => 32
            [email] => [email protected]
            [name] => Dan
        )

    [2] => Array
        (
            [R_ID] => 32
            [email] => [email protected]
            [name] => Paul
        )

    [3] => Array
        (
            [R_ID] => 35
            [email] => [email protected]
            [name] => Mike
        )  
)

I would like to insert this data into one table with each element value belonging to its respective field.

Currently my php code looks like the following

if(is_array($EMailArr)){
    foreach($EMailArr as $R_ID => $email => $name){

    $sql = "INSERT INTO email_list (R_ID, EMAIL, NAME) values ('$R_ID', '$email', '$name')";
    mysql_query($sql) or exit(mysql_error()); 
    }
}

*Note : R_ID is NOT the primary key in this table.*

Can someone help me understand how I should approach this situation? Thank you for reading and your help!

Regards.

This question is related to php mysql

The answer is


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;

Another way would be Prepared Statements, which are even more suited for your situation.


if(is_array($EMailArr)){
    foreach($EMailArr as $key => $value){

    $R_ID = (int) $value['R_ID'];
    $email = mysql_real_escape_string( $value['email'] );
    $name = mysql_real_escape_string( $value['name'] );

    $sql = "INSERT INTO email_list (R_ID, EMAIL, NAME) values ('$R_ID', '$email', '$name')";
    mysql_query($sql) or exit(mysql_error()); 
    }
}

A better example solution with PDO:

 $q = $sql->prepare("INSERT INTO `email_list` 
                     SET `R_ID` = ?, `EMAIL` = ?, `NAME` = ?");

 foreach($EMailArr as $value){
   $q ->execute( array( $value['R_ID'], $value['email'], $value['name'] ));
 }

I've a PHP library which helps to insert array into MySQL Database. By using this you can create update and delete. Your array key value should be same as the table column value. Just using a single line code for the create operation

DB::create($db, 'YOUR_TABLE_NAME', $dataArray);

where $db is your Database connection.

Similarly, You can use this for update and delete. Select operation will be available soon. Github link to download : https://github.com/pairavanvvl/crud