How to insert json array into mysql database

前端 未结 6 949
春和景丽
春和景丽 2020-12-05 11:46

Hi I\'m trying to insert the json array into my MySQL database. I\'m passing the data form my iphone there i have converted the data into json format and I\'m passing the da

相关标签:
6条回答
  • 2020-12-05 12:01
     $json = file_get_contents('php://input');
     $obj = json_decode($json,true);
    

    I think you are passing the wrong variable. You should pass $json in json_decode as shown above.

    0 讨论(0)
  • 2020-12-05 12:04

    You are missing JSON source file. Create a JSON file then assign it to var data:

    <?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);
    }
    
    ?>
    
    0 讨论(0)
  • 2020-12-05 12:06
    $string=mysql_real_escape_string($json);
    
    0 讨论(0)
  • 2020-12-05 12:13

    There is no such variable as $data. Try

    $obj = json_decode($json,true);
    

    Rest looks fine. If the error still persists, enable error_reporting.

    0 讨论(0)
  • 2020-12-05 12:18
    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 讨论(0)
  • 2020-12-05 12:27

    Its simple you can insert json datatype as below. reference link: click here for more details

    insert into sgv values('c-106', 'admin','owner',false,'[{"test":"test"}, 
    {"test":"test"}]',0,'pasds');
    
    0 讨论(0)
提交回复
热议问题