我的数组项结构看起来像
title: "",
createdBy: "",
pointerSet:[
{position: 1, catagoryId: 1, caption: "", imageUrl: ""}
{position: 1, catagoryId: 1, caption: "", imageUrl: ""}
{position: 1, catagoryId: 1, caption: "", imageUrl: ""}
]
现在,如果我的 URL 命中了该项目数组数据,那么我如何在数据库中插入pointerSet 数组项目。
我的数据库数据转到两个表
表一
title: "",
createdBy: "",
然后表二
pointerSet:[
{position: 1, catagoryId: 1, caption: "", imageUrl: ""}
{position: 1, catagoryId: 1, caption: "", imageUrl: ""}
{position: 1, catagoryId: 1, caption: "", imageUrl: ""}
]
我正在使用 get 方法。
在表一中我可以插入,但对于表二我该如何插入。
$_GET['pointerSet'] = array(
array(position='', catagoryId='',caption='',imageUrl=''),
array(position='', catagoryId='',caption='',imageUrl=''),
array(position='', catagoryId='',caption='',imageUrl='')
);
最佳答案
以下是如何使用 PDO 准备好的语句来做到这一点
$stmt = $pdo->prepare("INSERT INTO pointerTable (position, categoryid, caption, imageurl) VALUES(:position, :categoryid, :caption, :imageurl)");
foreach ($array['pointerSet'] as $pointer) {
$stmt->execute($pointer);
}
关于php mysql 从数组项插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26473899/