将php数组插入mysqli数据库行? [关闭]

将php数组插入mysqli数据库行?  [关闭]

问题描述:

In my MySQLi database, I have 2 columns in the table. One is 'author' and the other is 'books'. I have a variable called '$author' and an array called '$books' which contains unknown numbers of values.

I want to populate column 'books' with values inside array '$books' and in the other column variable '$author' which will remain constant in all rows.

Please help. It will be even more appreciated if you provide it in procedural way instead of OOP.

在我的MySQLi数据库中,表格中有2列。 一个是“作者”,另一个是“书籍”。 我有一个名为'$ author'的变量和一个名为'$ books'的数组,其中包含未知数量的值。 p>

我想用数组'$ books中的值填充列'books' '和另一列变量'$ author',它将在所有行中保持不变。 p>

请帮忙。 如果您以程序方式而不是OOP提供它,将会更加重要。 p> div>

Assuming $author is a string and $books is an array of integers,

$sql = "INSERT INTO your_table (books,author) VALUES(";
foreach($books as $book_data){

    $sql .= "($book_data,'$author'),";

}
$sql = rtrim($sql, ",") . ")";
//Execute the query

This will insert every $books variable as a new row with the constant $author.