How does the medoo framework insert JSON into the database?

Source: Internet
Author: User
At the beginning, I learned how to insert multiple data records in the insert file of medoo in PHP: {code...}. How can I insert post data into the database? Can you give a simple DEMO? Thank you! The data from post is roughly as follows: {code ...} I tried it many times... at the beginning, I learned how to insert multiple data entries in the medoo file insert:

$last_user_id = $database->insert("account", [    [        "user_name" => "foo",        "email" => "foo@bar.com",        "age" => 25,        "city" => "New York",        "(JSON) lang" => ["en", "fr", "jp", "cn"]    ],    [        "user_name" => "bar",        "email" => "bar@foo.com",        "age" => 14,        "city" => "Hong Kong",        "(JSON) lang" => ["en", "jp", "cn"]    ]]);

How can I insert post data to a database? Can you give a simple DEMO? Thank you!
The data from post is roughly as follows:

{    "name" : "xiaoming",    "age" : 20},{    "name" : "lihong",    "age" : 25}

I tried it many times but failed. I hope you can answer it. Thank you.

Reply content:

At the beginning, I learned how to insert multiple data entries in the medoo file insert:

$last_user_id = $database->insert("account", [    [        "user_name" => "foo",        "email" => "foo@bar.com",        "age" => 25,        "city" => "New York",        "(JSON) lang" => ["en", "fr", "jp", "cn"]    ],    [        "user_name" => "bar",        "email" => "bar@foo.com",        "age" => 14,        "city" => "Hong Kong",        "(JSON) lang" => ["en", "jp", "cn"]    ]]);

How can I insert post data to a database? Can you give a simple DEMO? Thank you!
The data from post is roughly as follows:

{    "name" : "xiaoming",    "age" : 20},{    "name" : "lihong",    "age" : 25}

I tried it many times but failed. I hope you can answer it. Thank you.

If it is not form submission, that is, the content-type of the request header is not equal to application/x-www-form-urlencoded or multipart/form-data, PHP cannot automatically parse the data you passed and assign the value to $ _ POST. You need to usephp://inputObtain all transmitted content and parse the data manually.

Assume that the data you sent is:

[    {"name": "xiaoming", "age": 20},    {"name": "lihong", "age": 25}]

So you can write:

$_POST = json_decode( file_get_contents('php://input'), true);$last_user_id = $database->insert("account", $_POST);

In addition to medoo, what other useful database tools does php provide?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.