0

Hi I'm trying to insert the json array into my MySQL database. but I have a problem if I insert my Json data and try to insert into my database, display in browser is empty. I dont know how to fix it, please tell me and help me for this.

Here its my json data

[{"id_peneliti":1083,"id_prodi":"4","nama_lengkap":"hasil edit","nama_tampilan":"duplikat","nip":"11111111222222222222","foto_user":"img\/foto\/2b8a1f40237e7f58bf0f7376fc8d5288.jpeg","email":"email@gmail.com","phone":"+39ry982835982","riwayat_pendidikan":"
sfbvxlkmblfkblkn<\/p>\r\n"}]

here is my parsing json

<?
 $json_url = "http://localhost/location/create_jsoninput.php";
 $json = file_get_contents($json_url);
 $data = json_decode($json,true);

 //Database Connection
mysql_connect("localhost","name","pass") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

    if (foreach ($data as $item)) {
       mysql_query("INSERT INTO tb_database VALUES ('".$item['id_peneliti']."','".$item['id_prodi']."', '".$item['nama_lengkap']."', '".$item['nama_tampilan']."', '".$item['nip']."', '".$item['password']."', 
       '".$item['name']."', '".$item['email']."')");
     mysql_close();
     echo "Data Berhasil Disimpan...";
     }else{
     echo "Data Gagal, Tersimpan...";
     }
?>

Please help me...

mfaqihdz
  • 17
  • 2
  • What errors are you getting, if any and if checking for them? `mysql_error()` and [**this... click me, I won't bite, too hard.**](http://php.net/manual/en/function.error-reporting.php) – Funk Forty Niner Nov 12 '14 at 17:04
  • Your JSON doesn't have multiple rows, you shouldn't be using `foreach`. – Barmar Nov 12 '14 at 17:05
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 12 '14 at 17:06
  • 1
    `if (foreach ...))` is not valid PHP syntax. How is this code even running? – Barmar Nov 12 '14 at 17:06

2 Answers2

1

You are iterating your data as it was an array, but it is not. Try:

<?
 $json_url = "http://localhost/location/create_jsoninput.php";
 $json = file_get_contents($json_url);
 $data = json_decode($json,true);

 //Database Connection
mysql_connect("localhost","name","pass") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

mysql_query("INSERT INTO tb_database VALUES ('".$data['id_peneliti']."','".$data['id_prodi']."', '".$data['nama_lengkap']."', '".$data['nama_tampilan']."', '".$data['nip']."', '".$data['password']."', 
       '".$data['name']."', '".$data['email']."')");
     mysql_close();
     echo "Data Berhasil Disimpan...";
?>
manuelmhtr
  • 326
  • 2
  • 5
  • still not work... I don't know whats wrong, but the display in my browser just empty. can you help... – mfaqihdz Nov 12 '14 at 17:20
0

Edit: try this instead:

if (!is_array($data) || count($data) != 1)
    exit(); // Error handling
$item = $data[0];
mysql_query("INSERT INTO tb_database
    VALUES ('".$item['id_peneliti']."','".$item['id_prodi']."',
    '".$item['nama_lengkap']."', '".$item['nama_tampilan']."',
    '".$item['nip']."', '".$item['password']."',
    '".$item['name']."', '".$item['email']."')");

That should be enough. But you should escape your datas before inserting, or use prepared statements with PDO to prevent SQL injection.

Goombi
  • 81
  • 4
  • Sorry, my data is an array... so what should I do... the problem is, not display any error that tell me that is work or not and I have check in my database, still not entered new data. – mfaqihdz Nov 12 '14 at 17:32
  • Okay, then, just add `$item = $data[0]`, but you must be sure there's always one element :) If it's still not working, edit your question with the output of `var_dump($data)`. – Goombi Nov 13 '14 at 09:32