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

5
  • 1
    Show code you have tried Commented Apr 13, 2015 at 6:54
  • 3
    Please check this link for help:- stackoverflow.com/questions/779986/… Commented Apr 13, 2015 at 6: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? Commented Apr 13, 2015 at 7: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. Commented Apr 13, 2015 at 7:36
  • Same problem on several other sites, appears that this poster has adopted a shotgun approach... Commented Apr 14, 2015 at 0:37

2 Answers 2

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).

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

Comments

-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);

}

2 Comments

please do not use mysql_ functions. Use mysqli_ instead. see: Why shouldn't I use mysql_* functions in PHP?
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!

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.