搜索
首页后端开发php教程medoo框架如何将JSON插入到数据库?

刚开始学习PHP,medoo的文档insert中提到的插入多条数据:

<code>$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"]
    ]
]);</code>

请教大神,该如何将post来的数据插入到数据库?能不能给个简单的DEMO?万分感谢!
post来的数据大致如下:

<code>{
    "name" : "xiaoming",
    "age" : 20
},
{
    "name" : "lihong",
    "age" : 25
}
</code>

我自己尝试了好多次,都没成功,希望大神能解答,谢谢

回复内容:

刚开始学习PHP,medoo的文档insert中提到的插入多条数据:

<code>$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"]
    ]
]);</code>

请教大神,该如何将post来的数据插入到数据库?能不能给个简单的DEMO?万分感谢!
post来的数据大致如下:

<code>{
    "name" : "xiaoming",
    "age" : 20
},
{
    "name" : "lihong",
    "age" : 25
}
</code>

我自己尝试了好多次,都没成功,希望大神能解答,谢谢

如果不是表单提交,也就是说 request header 的 content-type 不等于 application/x-www-form-urlencoded 或者 multipart/form-data 的话,PHP 是没办法自动解析你传递过来的数据并赋值到 $_POST 去的。这个时候你需要使用 php://input 获取所有传递过来的内容并手动解析数据。

假设你传过来的数据是:

<code>[
    {"name": "xiaoming", "age": 20},
    {"name": "lihong", "age": 25}
]
</code>

那么你可以这么写:

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

除了medoo以外,php还有什么好用的数据库工具类?

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
使用PHP发送电子邮件的最佳方法是什么?使用PHP发送电子邮件的最佳方法是什么?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依赖注入的最佳实践PHP中依赖注入的最佳实践May 08, 2025 am 12:21 AM

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

PHP性能调整技巧和技巧PHP性能调整技巧和技巧May 08, 2025 am 12:20 AM

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

PHP电子邮件安全性:发送电子邮件的最佳实践PHP电子邮件安全性:发送电子邮件的最佳实践May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

您如何优化PHP应用程序的性能?您如何优化PHP应用程序的性能?May 08, 2025 am 12:08 AM

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,数据库imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)优化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

PHP中的依赖注入是什么?PHP中的依赖注入是什么?May 07, 2025 pm 03:09 PM

依赖性注射inphpisadesignpatternthatenhancesFlexibility,可检验性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

最佳PHP性能优化技术最佳PHP性能优化技术May 07, 2025 pm 03:05 PM

PHP性能优化可以通过以下步骤实现:1)在脚本顶部使用require_once或include_once减少文件加载次数;2)使用预处理语句和批处理减少数据库查询次数;3)配置OPcache进行opcode缓存;4)启用并配置PHP-FPM优化进程管理;5)使用CDN分发静态资源;6)使用Xdebug或Blackfire进行代码性能分析;7)选择高效的数据结构如数组;8)编写模块化代码以优化执行。

PHP性能优化:使用OpCode缓存PHP性能优化:使用OpCode缓存May 07, 2025 pm 02:49 PM

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版