[英]How to insert Json into mysql database?
Hi I'm trying to insert the json array into my MySQL database. 嗨,我正在尝试将json数组插入我的MySQL数据库。 but I have a problem if I insert my Json data and try to insert into my database, display in browser is empty.
但是如果我插入我的Json数据并尝试将其插入数据库中时出现问题,则浏览器中的显示为空。 I dont know how to fix it, please tell me and help me for this.
我不知道该如何解决,请告诉我并为此提供帮助。
Here its my json data 这是我的json数据
[{"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
<?
$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... 请帮我...
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...";
?>
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.
但是您应该在插入之前转义数据,或者将准备好的语句与PDO一起使用以防止SQL注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.