medoo架構如何將JSON插入到資料庫?

來源:互聯網
上載者:User
剛開始學習PHP,medoo的文檔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"]    ]]);

請教大神,該如何將post來的資料插入到資料庫?能不能給個簡單的DEMO?萬分感謝!
post來的資料大致如下:

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

我自己嘗試了好多次,都沒成功,希望大神能解答,謝謝

回複內容:

剛開始學習PHP,medoo的文檔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"]    ]]);

請教大神,該如何將post來的資料插入到資料庫?能不能給個簡單的DEMO?萬分感謝!
post來的資料大致如下:

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

我自己嘗試了好多次,都沒成功,希望大神能解答,謝謝

如果不是表單提交,也就是說 request header 的 content-type 不等於 application/x-www-form-urlencoded 或者 multipart/form-data 的話,PHP 是沒辦法自動解析你傳遞過來的資料並賦值到 $_POST 去的。這個時候你需要使用 php://input 擷取所有傳遞過來的內容並手動解析資料。

假設你傳過來的資料是:

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

那麼你可以這麼寫:

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

除了medoo以外,php還有什麼好用的資料庫工具類?

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.