简体   繁体   English

如何从PHP数组插入多个MySQL表行

[英]How to insert multiple MySQL table rows from a PHP array

I'm new to both PHP and MySQL and I have a pretty complicated task to do for my current know-how. 我对PHP和MySQL还是陌生的,对于当前的专业知识,我有一项非常复杂的任务要做。 It goes like this: 它是这样的:

I'm trying to create a data-driven website with a hand made CMS that will allow a user with no knowledge of web design/web development to manage it. 我正在尝试用手工制作的CMS创建一个数据驱动的网站,该网站将允许不了解Web设计/ Web开发的用户来管理它。 Where I'm stuck is when I try to create a query to handle the data inserted to the database. 当我尝试创建查询来处理插入到数据库中的数据时,就会遇到麻烦。 It is a bookstore and I'm inserting books in the database. 这是一家书店,我正在向数据库中插入书籍。 I thought of creating 3 tables, one to store the books themselves, one to store the categories these books belong to (fe literature, mystery, fantasy, etc) and, since the relation between these two tables is many-to-many relationship, I also created a lookup table to link these two, using 2 columns populated with 2 foreign keys representing the IDs of the two tables. 我想创建3个表,一个表用于存储书籍本身,一个表用于存储这些书籍所属的类别(例如文学,神秘,幻想等),并且由于这两个表之间的关系是多对多关系,我还创建了一个查找表来链接这两个表,使用2列填充了2个代表两个表ID的外键。

The problem is I can't find how to insert multiple rows with same ID (the book ID) on one column and different second column (the category ID). 问题是我找不到如何在同一列和不同的第二列(类别ID)上插入具有相同ID(书ID)的多行。 The categories will be picked in an HTML form via checkboxes. 将通过复选框以HTML形式选择类别。 I' ve seen in several posts here I need to make an array in PHP script to store the data from the checkboxes the user picked, but I have absolutely no clue on how to structure the query that will turn these checkboxes into multiple MySQL table rows. 我在这里看到过几篇文章,我需要在PHP脚本中创建一个数组来存储用户选择的复选框中的数据,但是我绝对不知道如何构造将这些复选框变成多个MySQL表行的查询。

I've seen people suggesting imploding the values, but that would store multiple values in one row, or am I mistaken? 我见过有人建议内插值,但这会在一行中存储多个值,还是我弄错了? Because that is not what I want, I want (if fe the user checked 3 categories checkboxes) to create 3 rows in the lookup table. 因为这不是我想要的,所以我想要(如果用户选中了3个类别复选框)在查找表中创建3行。

Thanks in advance for your time seeing my question. 预先感谢您拨冗看我的问题。

$categories = array(1,2,3,4);
$bookId = 5;

foreach ($categories as $categoryId) {

    $data = array(
        'book_id' => (int)$bookId,
        'category_id' => (int)$categoryId
    );

    $query = "INSERT INTO `lookup` SET ";
    $fields = array();
    foreach ($data as $field => $value) {
        $fields[] = "`$field` = '$value'";
    }
    $fields = implode(', ', $fields);
    $query .= $fields;

    mysql_query($query);
}

You can build up an insert using implode to insert many rows. 您可以使用implode插入许多行来建立一个插入。 Something like this:- 像这样的东西:

<?php

$Category = 123;

$Books = array(1,2,3,4,5,6,7,8,9,10);

$sql = "INSERT INTO book_category_link(NULL, category_id, book_id)
        VALUES ($Category".implode("),($category", $Books).")";

?>

Or if you want to use the existing tables to get the ids then something like this:- 或者,如果您想使用现有的表格来获取ID,则应如下所示:

<?php

$Category = (123, 456, 789);

$Books = array(1,2,3,4,5,6,7,8,9,10);

$sql = "INSERT INTO book_category_link(NULL, category_id, book_id)
        SELECT NULL, categories.id, books.id
        FROM books
        CROSS JOIN categories
        WHERE books.id IN (".implode(",", $Books).")
        AND categories.id IN (".implode(",", $Category).")";

?>

No, you can't insert into multiple tables in one MySQL command. 不,您不能在一个MySQL命令中插入多个表。 You can however use transactions. 但是,您可以使用事务。

INSERT ...
SELECT LAST_INSERT_ID() INTO @mysql_variable_here;
INSERT INTO table2 (@mysql_variable_here, ...);
INSERT INTO table3 (@mysql_variable_here, ...);

if you want to add multiple rows inserted then u can use the syntax 如果要添加插入的多行,则可以使用以下语法

INSERT INTO  table (col1, col2) VALUES ('value', 'value'), (value, 'value');

It's really simple. 真的很简单。 First you insert the the book into the books table and then get the ID of it . 首先,将书插入books表中,然后获取它的ID Let's say for this example it is the ID 5 . 假设此示例为ID 5

Lets say you have the following categorie id's from the checkboxes: array(21, 42, 64); 假设您在复选框中具有以下类别ID: array(21, 42, 64); Now you can construct a query like this to insert them into the lookup table: 现在,您可以构造一个查询,将其插入到lookup表中:

INSERT INTO `lookup` (book_id, category_id') VALUES (5, 21), (5, 42), (5, 64);

The very basic idea is to iterate a loop on the categories submitted and run an insert query containing category ID and book ID into the lookup table. 最基本的想法是对提交的类别进行循环,并在查询表中运行包含类别ID和书籍ID的插入查询。

$bookID= $_POST['bookid'];
foreach ($categories as $category) {

    mysql_query("INSERT INTO lookup(bookid,categoryid) values($bookID,$catetory)");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM