2

I am trying to insert data from that array into my database with sql.

foreach ($array as $row){
    mysql_query($link,"INSERT INTO table (name, surname) VALUES('"$array[0][name]"', '"$array[0][surname]"')}
John
  • 57
  • 7
  • [`mysqli_error`](http://php.net/manual/en/mysqli.error.php) and use [prepared statements](http://php.net/manual/en/mysqli.prepare.php). – Script47 Jun 23 '18 at 17:20
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Jun 23 '18 at 17:28
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Jun 23 '18 at 17:29
  • You have an extra " in `, '"$someArray[brand_name]` – Nigel Ren Jun 23 '18 at 18:07

1 Answers1

2

You are calling object instead of array. You should call row['name'] instead of $array['name'].

foreach ($array as $row){
    mysql_query($link,"INSERT INTO table (name, surname) VALUES('$row[name]', '"$row[surname]'"); 
};
John
  • 57
  • 7
Rumesh
  • 402
  • 4
  • 15
  • 2
    Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Jun 23 '18 at 17:29
  • True. I normally use PDO with parameter binding. For this instance I just gave the visible solution. ;-) – Rumesh Jun 23 '18 at 17:53
  • There is an additional quotation mark before $row[brand_name]. check that – Rumesh Jun 23 '18 at 18:05
  • Try This : foreach ($someArray as $row){ mysqli_query($mysqli,"INSERT INTO dresses (name, brand_name, price, linke_url, offer) VALUES('$row[name]', '$row[brand_name]', '$row[price]', '$row[link_url]', '$row[offer]')"); }; – Rumesh Jun 23 '18 at 18:09
  • it is fine. because inside of foreach you can access each array directly. as an example in first loop you can access first array. Second loop second array like that. so that is not the issue. something wrong with your sql. check for not nulluble columns left on your db table (not inserting column). – Rumesh Jun 23 '18 at 18:37
  • use below code and show me output – Rumesh Jun 23 '18 at 18:49
  • $result = mysqli_query($mysqli,"INSERT INTO dresses (name, brand_name, price, linke_url, offer) VALUES('$row[name]', '$row[brand_name]', '$row[price]', '$row[link_url]', '$row[offer]')"); echo $result. '
    ';
    – Rumesh Jun 23 '18 at 18:50