Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello i have a JSon array like this

   Array
  (
    [NTAy] => ALFA ROMEO
    [NTA0] => AUDI
    [NTEx] => BMW
    [NjAy] => CHEVROLET
    [NTEz] => CHRYSLER
    [NTE0] => CITROËN
    [NjAz] => DACIA
    [NjQ5] => DAEWOO
    [NTE3] => DAIHATSU
  )

and I have to insert it in a database row by row, I used

foreach ($mata as $marca_id => $marca_name){
    $line = $line. "','" . $marca_id . "','". $marca_name . "','". $marca_name;
}
$line = substr($line, 0, strlen($line)-1);
$values[$i] = $line;
++$i;

$values =implode ( $values);
echo $values;
echo "<br />";

but at the mysql insert

$data = mysql_query(" INSERT INTO jos_virtuemart_categories_ro_ro (virtuemart_category_id, category_name, slug) 
VALUES " .$values)

I get an error and can't get it to work. Can someone help me please?

share|improve this question
1  
What's $i doing outside the foreach loop? What do you want the query to be? –  Amal Murali Oct 17 '13 at 14:16
    
@AmalMurali I think OP does not have a proper understanding of how INSERT statements work, nor loops. –  BadHorsie Oct 17 '13 at 14:32

2 Answers 2

foreach ($mata as $marca_id => $marca_name) {
    $id = intval($marca_id);
    $name = mysql_real_escape_string($marca_name);
    $values = "$id, '$name', '$name'";
    mysql_query("INSERT INTO jos_virtuemart_categories_ro_ro (virtuemart_category_id, category_name, slug) VALUES ($values)");
}
share|improve this answer

Why not use prepared statements? Especially if you're taking user generated data and entering it into your database.

That and it looks like you're final SQL query will look like this

INSERT INTO jos_virtuemart_categories_ro_ro (virtuemart_category_id, category_name, slug) 
VALUES 'NTAy','ALFA ROMEO','ALFA ROMEO','NTA0','AUDI','AUDI' etc

You would have to break this up into multiple queries

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.