在mysql中插入多行(用逗号分隔的项目) [英] Insert multiple rows into mysql (items separated by comma)
问题描述
我有一个小问题:)我正在网上搜索,但未找到任何解决方案.
I have a small problem :) I was searching the web but didn't find any solutions.
我有一个像这样的值(从$ _GET []中获取)
I have a value like this (got it from $_GET[])
tag1,tag2,tag3,tag4
tag1, tag2, tag3, tag4
所有我想做的就是像这样将其插入MySql(通过PHP):
and all I want to do is insert it into MySql (via PHP) like this:
+---------+-------------+
| id | tag |
+---------+-------------+
| 33 | tag1 |
| 33 | tag2 |
| 33 | tag3 |
| 33 | tag4 |
+---------+-------------+
我在Stack Overflow上找到了很多与此有关的文章,但是这里最大的问题是我不知道每次都会获得多少值(我没有找到答案).
I found many articles about this on Stack Overflow but the biggest problem here is that I don't know how many values I'm gonna get each time (I did not find that answer).
我将不胜感激.
推荐答案
来自英国的问候:)
您不能只遍历querystring字段并一次向数据库添加一行吗?
Hi greetings from UK :)
Can't you just loop through the querystring fields and add one row to the database at a time?
例如
<?php
$id = 33;
$value_list = 'tag1,tag2,tag3,tag4';
$values = explode(',', $value_list);
foreach ($values as $value)
{
$sql = "INSERT INTO table (id, value) VALUES ($id, '$value');";
//.. execute SQL now
echo '<p>' . $sql . '</p>';
}
?>
我将此内容上传到 http://cyba.co/test.php ,这样您就可以看到输出.
I uploaded this to http://cyba.co/test.php so you can see the output.
这篇关于在mysql中插入多行(用逗号分隔的项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!