1

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?

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

2 Answers 2

1
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)");
}
0

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

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.