剛開始學習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還有什麼好用的資料庫工具類?