How to insert json array into mysql database

Hi, I am trying to insert a json array into my MySQL database. I submit the data form of my iphone, there I converted the data to json format, and I transfer the data to my server using a URL that it does not insert into my server.

This is my json data.

[{"Name": "0", "phone": "DSF", "city": "sdfsdf", "email": "DSF"}, {"name": "13123123", "phone": " sdfsdfdsfsd "," city ":" sdfsf "," email ":" 13123123 "}]

This is my php code.

<?php 

 $json = file_get_contents('php://input');
 $obj = json_decode($data,true);

 //Database Connection
require_once 'db.php';

 /* insert data into DB */
    foreach($obj as $item) {
       mysql_query("INSERT INTO `database name`.`table name` (name, phone, city, email) 
       VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

     }
  //database connection close
    mysql_close($con);

   //}
   ?>

My database connection code.

   <?php

       //ENTER YOUR DATABASE CONNECTION INFO BELOW:
         $hostname="localhost";
         $database="dbname";
         $username="username";
         $password="password";

   //DO NOT EDIT BELOW THIS LINE
     $link = mysql_connect($hostname, $username, $password);
     mysql_select_db($database) or die('Could not select database');
 ?> 

, . php. , php , , , .

+6
7
 $json = file_get_contents('php://input');
 $obj = json_decode($json,true);

, . $json json_decode, .

+7

, $data.

$obj = json_decode($json,true);

. - , error_reporting.

+4

JSON. JSON var:

<?php

require_once('dbconnect.php');

// reading json file
$json = file_get_contents('userdata.json');

//converting json object to php associative array
$data = json_decode($json, true);

// processing the array of objects
foreach ($data as $user) {
    $firstname = $user['firstname'];
    $lastname = $user['lastname'];
    $gender = $user['firstname'];
    $username = $user['username'];

    // preparing statement for insert query
    $st = mysqli_prepare($connection, 'INSERT INTO users(firstname, lastname, gender, username) VALUES (?, ?, ?, ?)');

    // bind variables to insert query params
    mysqli_stmt_bind_param($st, 'ssss', $firstname, $lastname, $gender, $username);

    // executing insert query
    mysqli_stmt_execute($st);
}

?>
+3

You can use it -> http://convertjson.com/json-to-sql.htm This is a JSON to SQL converter, and then you can upload your sql file generated from JSON to your database using PhpMyAdmin.

0
source

You can always use MySQL JSON functions and data type.

https://dev.mysql.com/doc/refman/8.0/en/json.html

0
source
header("Access-Control-Allow-Origin: http://localhost/rohit/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

//files needed to connect to database
include_once 'config/database.php';
include_once 'objects/main.php';

//get Database connection
$database = new Database();
$db = $database->getConnection();

//instantiate product object
$product = new Product($db);

//get posted data
$data = json_decode(file_get_contents("php://input"));

//set product property values
$product->b_nm = $data->Business_Name;
$product->b_pno = $data->Business_Phone;
$product->b_ads = $data->Business_Address;
$product->b_em = $data->Business_Email;
$product->b_typ = $data->Business_Type;
$product->b_ser = $data->Business_Service;

$name_exists = $product->nameExists();

if($name_exists){

    echo json_encode(
            array(
                "success"=>"0",
                "message" => "Duplicate Record Not Exists."

            )
        );

}   else{

        if($product->b_nm != null && $product->b_pno != null && $product->b_ads != null && $product->b_em != null && $product->b_typ != null && $product->b_ser != null){

                if($product->create()){
                //set response code
                http_response_code(200);

                //display message: Record Inserted
                echo json_encode(array("success"=>"1","message"=>"Successfully Inserted"));
            }

        }   
        else{

            //set response code
            http_response_code(400);

            //display message: unable to insert record
            echo json_encode(array("success"=>"0","message"=>"Blank Record Not Exists."));
        }
    }
0
source
$string=mysql_real_escape_string($json);
-2
source

Source: https://habr.com/ru/post/1532597/


All Articles