简体   繁体   English

PHP将多个数组插入MySQL数据库

[英]PHP insert multiple array into MySQL database

I have add/remove input box using jQuery for add video link and add into database like this: 我有使用jQuery添加/删除输入框来添加视频链接并将其添加到数据库中,如下所示:

<input name="video[]" class="" value="" />
<input name="video[]" class="" value="" />

I check and filter empty value than insert not empty value into my database like this: 我检查并过滤空值,而不是像这样在数据库中插入非空值:

$id = mysql_insert_id();

foreach(array_filter($_POST['video']) as $video_url) {
if (!empty($video_url)) {


$value['video_data'] = serialize((array(
array_filter($_POST['video'])
)));


SQL::insert("INSERT INTO " . NEWS_FILES . " (url, id,type) VALUES (?, ?, ?)", $value['video_data'], $id, "video");
}
}

Now, in database I see two row for id: 现在,在数据库中,我看到id的两行:

在此处输入图片说明

BUT, I need to insert one row for each id. 但是,我需要为每个ID插入一行。

How do can I fix this ? 我该如何解决?

edit: 编辑:

$id = mysql_insert_id();

Have you marked the id field in the database table as Auto Increment or not? 您是否已将数据库表中的id字段标记为“自动增量”?

Because as per the current code, the $id is not updated anywhere. 因为按照当前代码,$ id不会在任何地方更新。 So it assumes the default value and inserts. 因此,它采用默认值并插入。

I would suggest to initialize $id before starting the foreach loop and then doing an increment after the SQL statement is executed 我建议在开始foreach循环之前先初始化$ id,然后在执行SQL语句后进行增量

SQL::insert("INSERT INTO " . NEWS_FILES . " (url, id,type) VALUES (?, ?, ?)", $value['video_data'], $id, "video");
$i++;

You must create array values by php $ar = new array(); 您必须通过php $ ar = new array();创建数组值。 $ar[]= null; $ ar [] = null; You submit form data multipart load insert command by mysqldata "Insert data into table db " " for numbers all rows"; 您可以通过mysqldata“将数据插入表db”中的“为所有行编号”提交表单数据多部分加载插入命令;

If you dont have auto increment of id, you can use 如果没有自动递增的ID,则可以使用

$id = "select max(id) from  " .NEWS_FILES ; // execute sql and get maxid

than in your foreach add at a top $id++; 比在您的foreach中添加最高$ id ++;

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

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