Insert php array in mysql

I have an array of $ product_array, and when I use print_r ($ product_array) ;. The array shows this as

Array
(
    [0] => Array
        (
            [ID] => P00100
            [NAME] => Edina
            [PRICE] => $20.00
        )

    [1] => Array
        (
            [ID] => P00101
            [NAME] => Richfield
            [PRICE] => $21.00
        )

    [2] => Array
        (
            [ID] => P00102
            [NAME] => Bloomington
            [PRICE] => $22.00
        )
)

I set up the database table in 4 columns, the first of which is mainid, and which is an automatic increment, next with the identifier, NAME, PRICE, as shown above. I would like to insert this $ product_array array into mysql. Can anyone help? I would really appreciate it! Tks.

+5
source share
2 answers
   $sql = array(); 
    foreach( $myarray as $row ) {
        $sql[] = '('.$row['ID'].', "'.mysql_real_escape_string($row['NAME']).'",
 "'.$row['PRICE'].'")';
    }
    mysql_query('INSERT INTO table (ID, NAME,PRICE) VALUES '.implode(',', $sql));

see more details:

insert multiple rows through php array in mysql

+18
source

You can try this code (fast 'n' dirty):

foreach($product_array as $v) {
  $query = 'insert into tablename values (null, \'' . $v['id'] . '\', \'' . $v['name'] . '\', ' . $v['price'] . ');'
  mysql_query($query);
}
0
source

All Articles