-2

I'm beginner programming. I try to insert array value into mysql table. here is my array:

$responseArray=Array
(
    [0] => Array
        (
            [code] => 9BP3
            [name] => 9Bp No3
        )

    [1] => Array
        (
            [code] => AA
            [name] => Ataria
        )

    [2] => Array
        (
            [code] => AABH
            [name] => Ambika Bhawani Halt
        )

    [3] => Array
        (
            [code] => AADR
            [name] => Amb Andaura
        )

    [4] => Array
        (
            [code] => AAG
            [name] => Angar
        )

    [5] => Array
        (
            [code] => AAH
            [name] => Itehar
        )
)

and here is mysql table structure:

id, code, name

How to insert array into this table using loop?

and If in database table row differ from array count then it will truncate table and insert array.

Bikash Mahata
  • 35
  • 3
  • 9
  • 1
    Show code you have tried – CodeWithCoffee Apr 13 '15 at 06:54
  • 3
    Please check this link for help:- http://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql – Alive to die - Anant Apr 13 '15 at 06:55
  • 1
    Do you know how to insert *one* row? Then what's your issue applying that to *many* rows in a loop? Also, I have no idea what you're trying to say with your last sentence. Truncate what when why? – deceze Apr 13 '15 at 07:25
  • deceze@First check that how many rows already in database, and it compare with array count. If two value differ then truncate table and insert array value into database. – Bikash Mahata Apr 13 '15 at 07:36
  • Same problem on several other sites, appears that this poster has adopted a shotgun approach... – Mark Giblin Apr 14 '15 at 00:37

2 Answers2

1

Here is a very basic example making hand-crafted SQL queries:

$aValues = array();
// Don't forget to protect against SQL injection :)
foreach($responseArray as $row){
    $aValues[] = '("'.$row['code'].'","'.$row['name'].'")';
}
$sql = 'INSERT INTO table (code, name) VALUES '.implode(',',$aValues).';';

But of course it all depends on what MySQL driver / DAL you might be using (e.g. PDO would be better to learn, but might be harder for a beginner).

scipilot
  • 6,681
  • 1
  • 46
  • 65
-1
foreach($responseArray as $individual_data)
{
   //Assign the values
   $name = $individual_data['name'];
   $code = $individual_data['code'];      

   //Insert into the DB
   mysql_query("insert into table_name (code,name) values ('".$code."','".$name."')",$conn);

}
Manoj K
  • 3
  • 5
  • 1
    please do not use _`mysql_`_ functions. Use _`mysqli_`_ instead. see: [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Ryan Vincent Apr 13 '15 at 08:47
  • Not only does this code use the deprecated mysql extension, it's also open to SQL injection. Using a deprecated extension and not escaping input is a very bad idea! – Oldskool Apr 13 '15 at 09:00