简体   繁体   English

无法将多个json数据插入mysql数据库表

[英]multiple json data into mysql database table not inserting

I have this code. 我有这个代码。

<?php
// open mysql connection
$host = "localhost";
$username = "root";
$password = "";
$dbname = "jacklin";
$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));
// use prepare statement for insert query
$st = mysqli_prepare($con, 'INSERT INTO company_details(com_name, city, com_address, com_mno, com_lno, com_faxno, com_email, com_url, contact_person, com_img,
 lat, lng, cat_src_pos, state, country, password, status, plan, token, pin, contact_person1, contact_person2,
  com_mno1, com_mno2, fpass_token, adv_src_pos, alias, com_skype, cover)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'ssssssssssssissssssssssssisss', $id, $city, $com_address, $com_mno, $com_lno, $com_faxno, $com_email, $com_url, $contact_person, $com_img, $lat, $lng, $cat_src_pos, $state, $country, $password, $status, $plan, $token, $pin, $contact_person1, $contact_person2, $com_mno1, $com_mno2, $fpass_token, $adv_src_pos, $alias, $com_skype, $cover);
// read json file
$filename = 'empdata.json';
$json = file_get_contents($filename);   
//convert json object to php associative array
$data = json_decode($json, true);
// loop through the array
foreach ($data as $row) {
    // get the employee details
    $id = $row['com_name'];
    $city = $row['city'];
    $com_address = $row['com_address'];
    $com_mno = $row['com_mno'];
    $com_lno = $row['com_lno'];
    $com_faxno = $row['com_faxno'];
    $com_email = $row['com_email'];
    $com_url = $row['com_url'];
    $contact_person = $row['contact_person'];
    $com_img = $row['com_img'];
    $lat = $row['lat'];
    $lng = $row['lng'];
    $cat_src_pos = $row['cat_src_pos'];
    $state = $row['state'];
    $country = $row['country'];
    $password = $row['password'];
    $status = $row['status'];
    $plan = $row['plan'];
    $token = $row['token'];
    $pin = $row['pin'];
    $contact_person1 = $row['contact_person1'];
    $contact_person2 = $row['contact_person2'];
    $com_mno1 = $row['com_mno1'];
    $com_mno2 = $row['com_mno2'];
    $fpass_token = $row['fpass_token'];
    $adv_src_pos = $row['adv_src_pos'];
    $alias = $row['alias'];
    $com_skype = $row['com_skype'];
    $cover = $row['cover']   
    // execute insert query
    mysqli_stmt_execute($st);
}
//close connection
mysqli_close($con);
?>

and my empdata.json is like. 我的empdata.json就像。

[{"com_id":"1","com_name":"SORENTO GRANITO PVT.LTD","city":"Morbi","com_address":"8-A National High WayOld ghuntu Road ,Morbi - 363 642 (Guj.) INDIA","com_mno":"+919377721600","com_lno":"02822 - 243783 \/ 84","com_faxno":"(02822) 243785","com_email":"marketing@sorentogranito.com","com_url":"www.sorentogranito.com","contact_person":"Mr. Bhagubhai Tulsiyani","com_img":"1403952411.png","lat":"22.824254","lng":"70.8606801","cat_src_pos":"400000","state":"Gujarat","country":"India","password":"91SORESGPL","status":"active","plan":"premium","token":"","pin":"363 642","contact_person1":"","contact_person2":"","com_mno1":"","com_mno2":"","fpass_token":"","adv_src_pos":"400000","alias":"sorento-granito-pvt-ltd","com_skype":"","cover":"motto.jpg"},{"com_id":"3","com_name":"COTO CERAMIC PVT LTD","city":"Morbi","com_address":"8-A National Higway,B\/ h Makansar Panjarapore Weed...","com_mno":"+919099173713","com_lno":"+919099173713","com_faxno":"","com_email":"info@cotobathware.com","com_url":"www.cotobathware.com","contact_person":"Mr. SUMEET MARVANIYA","com_img":"d08687ba60bb3f0d1317e2fd8b10afd4.png","lat":"22.748123","lng":"70.9369573","cat_src_pos":"500000","state":"Gujarat","country":"India","password":"MAYANK8877","status":"active","plan":"basic","token":"","pin":"363621","contact_person1":"","contact_person2":"","com_mno1":"","com_mno2":"","fpass_token":"","adv_src_pos":"500000","alias":"coto-ceramic-pvt-ltd","com_skype":"","cover":"motto.jpg"},{"com_id":"4","com_name":"GLORY CERAMIC PVT LTD","city":"Morbi","com_address":"8\/A , National Highway Lalpar Morbi","com_mno":"+919825228848","com_lno":"02822 - 650445\/ 652446","com_faxno":"","com_email":"gloryceramic@yahoo.co.in","com_url":"www.gloryceramic.com","contact_person":"Mr. Niraj Thakkar","com_img":"1403952443.png","lat":"22.7968786","lng":"70.8907196","cat_src_pos":"80000","state":"Gujarat","country":"India","password":"9227650445","status":"active","plan":"premium","token":"","pin":"363 641","contact_person1":"","contact_person2":"","com_mno1":"","com_mno2":"","fpass_token":"","adv_src_pos":"80000","alias":"glory-ceramic-pvt-ltd","com_skype":"","cover":"motto.jpg"},{"com_id":"5","com_name":"SALON CERAMIC PVT.LTD.","city":"Morbi","com_address":"8-A National Highway, Olg Ghuntu Road, Morbi - 363 642(Guj.) INDIA","com_mno":"+91 9825223840","com_lno":"+91 2822 242115","com_faxno":"+91 2822 242116","com_email":"info@salonceramic.com","com_url":"www.salonceramic.com","contact_person":"Mr. Hiteshbhai","com_img":"1413397071.PNG","lat":"22.838649048614528","lng":"70.88279977525485","cat_src_pos":"400000","state":"Gujarat","country":"India","password":"123salon123","status":"active","plan":"premium","token":"252985240685b6f5b1728d0d31bc585b","pin":"363642","contact_person1":"","contact_person2":"","com_mno1":"","com_mno2":"","fpass_token":"","adv_src_pos":"400000","alias":"salon-ceramic-pvt-ltd","com_skype":"","cover":"motto.jpg"}]

with 1000 of records.but when i run above code in my localhost it's not displaying any error and not inserting any record to database too. 有1000条records。但是当我在本地主机中运行以上代码时,它不会显示任何错误,也不会在数据库中插入任何记录。 please tell me how to insert this type json to database. 请告诉我如何将这种类型的json插入数据库。

Try a simple foreach loop to count the number of data, and then make a for loop for running the insert statement: 尝试使用简单的foreach循环来计算数据的数量,然后为运行insert语句创建一个for循环:

$file = file_get_contents('empdata.json');
$json = json_decode($file, true);
//echo '<pre>';
//print_r($json);    
$num = array();//Open Blank array for number of data
foreach($json as $k => $v):
    $num [] = $v; //number of data        
endforeach;
$row= count($num);//Put number of data in $row
for($i=0; $i<$row; $i++){
   //instead of putting the value in the statement, store them in variable
   $com_name = $json[$i]['com_name'];
   $city = $json[$i]['city'];
   $com_address = $json[$i]['com_address'];
   //Put the other variable like this and put them in insert statement
   $stmt = mysqli_prepare($con, "INSERT INTO company_details VALUES (?, ?,?)");
   mysqli_stmt_bind_param($stmt, 'sss', $com_name, $city, $com_address);
}

This is just a simple working solution( working demo with real database ). 这只是一个简单的工作解决方案( 使用实际数据库进行演示 )。 I don't have no time for typing all the fields. 我没有时间输入所有字段。 For the second and third loop, there is no fax number, so if the data field default value cannot have null value, there will be some problem and your statement will not work properly and likely fails. 对于第二个和第三个循环,没有传真号,因此如果数据字段的默认值不能为空值,则会出现一些问题,您的语句将无法正常运行并可能失败。 I hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM