php mysql 从数组项插入

标签 php mysql arrays

我的数组项结构看起来像

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/

相关文章:

php - 在执行员工搜索之前我收到以下错误

PHP 添加新行\n 到 preg_replace_callback 函数中

python - SQLalchemy Core,检索更新行的 ID

javascript - 函数,返回最后一个值等于某个值的对象。 (JavaScript)

php - jQuery ajax,发送数据

php - 我如何使用增量使用 php mysql 更新我的表列

php - ON DUPLICATE KEY UPDATE 上的 SQL 语法错误

php - PHP-MySQL 或 MySQLi 中哪个最快?

arrays - KRL : Length of an array

java - 如何在特定字符之前分割字符串(以便分隔符位于结果的第二部分)