【问题标题】:How to Insert Json Data in Mysql with PHP如何使用 PHP 在 Mysql 中插入 Json 数据
【发布时间】:2020-10-25 04:41:11
【问题描述】:

我想用 PHP 在 Mysql 的单列中插入 JSON 数据

<input type="text" name="text1" />
<input type="text" name="text2" />
$jsondata = '[{"value1":{ "text":"text1", "text2":"text2"},"value1":{ "text":"text1", "text2":"text2"}}]';
INSERT INTO tablename (row) VALUES ($jsondata);

有可能

或者我们可以生成一个 JSON 文件然后导入请指导我

谢谢

【问题讨论】:

    标签: php mysql arrays json


    【解决方案1】:

    检查此代码可能会对您有所帮助:


    1 在 PHP 中读取 JSON 文件:

    PHP supports the function file_get_contents() which will read the entire file and returns it as a string.

    //read the json file contents 
    $jsondata = file_get_contents('yourjsonfilename.json');
    

    2 将 JSON 字符串转换为 PHP 数组:

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

    The first parameter $jsondata contains the JSON file contents. The second parameter true will convert the string into PHP associative array.


    3 解析 PHP 数组值并存储在变量中:

    //get the details
    $id = $data['Id'];
    $name = $data['personal']['name'];
    $age = $data['personal']['age'];
    $streetaddress = $data['personal']['address']['streetaddress'];
    $city = $data['personal']['address']['city'];
    $state = $data['personal']['address']['state'];
    $postalcode = $data['personal']['address']['postalcode'];
    

    4 使用 PHP 代码将 JSON 插入 MySQL 数据库现在我们将插入 使用以下查询将 JSON 对象值提取到 MySQL 表中。

    //insert into mysql table
    $sql = "INSERT INTO tbl_students(studentId, name, age, streetaddress, city, state, postalcode)
        VALUES('$id', '$name', '$age', '$streetaddress', '$city', '$state', '$postalcode')";
    if(!mysqli_query($con, $sql))
    {
        die('Error : ' . mysql_error());
    }
    

    现在我们已经成功将 JSON 数据导入 MySQL 数据库,如下所示:

    {
        "Id": "ST001",
        "personal": {
            "name": "John Smith",
            "age": "29",
            "address": {
                "streetaddress": "5 14th Street",
                "city": "New York",
                "state": "NY",
                "postalcode": "12548"
            }
        }
    }
    

    另一种方式是:

    <?php 
    
    $josndata = '{
    "student":{ "name":"Harry", "country":"United State", "ContactNo":2545454 }
    }';  
    
    // Database connection
    $conn = new mysqli('localhost', 'username', 'password', 'databasename');
    
    // Insert data Query
    $sql = "INSERT INTO student_table ( name, jsondata )
    VALUES ('Harry', '$josndata')";
    
    if ($conn->query($sql) === TRUE) {
      echo "Insert your JSON record successfully";
    } 
    ?>
    

    【讨论】:

    • 只想在表格行学生、student_value import student={name=name,age=1,class=2,etc value} 等列上导入,而不是多个字段
    • 检查我在答案中添加的另一个解决方案
    • 它不起作用 $sql = "INSERT INTO student_table (name) VALUES ($josndata)";
    • 查询是 $sql = "INSERT INTO student_table (name, jsondata) VALUES ('Harry', '$josndata')"
    • 我们可以从表单创建 JSON 数据相同的页面然后插入 $josndata = '{by form submit}';
    【解决方案2】:
    1. 您可以在php中通过以下查询读取json数据。

      $testdata = file_get_contents('php://input');

    2. 然后将 JSON 数据转换为数组。

      $arrdata = json_decode($testdata, TRUE);

      $test1 = $arrdata['test1']; $test2 = $arrdata['test2']; $test3 = $arrdata['test3'];

    3. 最后你可以根据需要将数据插入到MYSQL表中。

      $sql = INSERT INTO tbl_test (test1 , test2 , test3) 值 ('$test1', '$test2', '$test3'); $result = mysqli_query($db,$sql);

    这是一个例子。您可以根据需要修改数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-13
      • 2020-12-25
      • 2019-09-07
      • 2013-12-31
      • 1970-01-01
      • 2017-08-06
      • 2017-10-07
      • 2012-07-21
      相关资源
      最近更新 更多