I have a small problem :) I was searching the web but didn't find any solutions.
我有一个小问题:)我在网上搜索,但是没有找到任何解决办法。
I have a value like this (got it from $_GET[])
我有一个这样的值(从$_GET[]获得)
tag1, tag2, tag3, tag4
标签1,标签2,tag3,从
and all I want to do is insert it into MySql (via PHP) like this:
我要做的就是把它插入MySql(通过PHP)
+---------+-------------+ | id | tag | +---------+-------------+ | 33 | tag1 | | 33 | tag2 | | 33 | tag3 | | 33 | tag4 | +---------+-------------+
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).
我在Stack Overflow上找到了很多关于这个的文章,但是这里最大的问题是我不知道每次会得到多少值(我没有找到那个答案)。
I will appreciate any help I get.
我将感激我得到的任何帮助。
7 个解决方案
#1
3
Hi greetings from UK :)
Can't you just loop through the querystring fields and add one row to the database at a time?
嗨,来自英国的问候语:)难道你不能通过querystring字段循环一次向数据库添加一行吗?
e.g.
如。
<?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>';
}
?>
I uploaded this to http://cyba.co/test.php so you can see the output.
我上传了这个到http://cyba.co/test。可以看到输出。
#2
1
You can do an INSERT INTO
with multiple values:
可以用多个值插入:
INSERT INTO table (tag) VALUES ('tag1', 'tag2', 'tag3');
Make sure, you properly escape your input (if user suplied) to prevent SQL injection. You can use mysql_escape_string()
for this.
确保正确地转义输入(如果用户支持)以防止SQL注入。您可以为此使用mysql_escape_string()。
Edit: To get these values, you can do:
编辑:为了得到这些值,你可以这样做:
$values = explode(",", $_GET['your_param']);
foreach($values as $idx => $val) {
$values[$idx] = mysql_real_escape_string(trim($val));
}
mysql_query("INSERT INTO table (tag) VALUES (".implode(",", $values).");");
#3
0
Use explode()
function to split the string
函数的作用是:分割字符串
$tag = "tag1, tag2, tag3";
$pieces = explode(", ", $tag);
SQL query would be
SQL查询将
INSERT INTO myTable VALUES ($pieces[0],$pieces[1],$pieces[2]);
Good Luck!!!
祝你好运! ! !
#4
0
I would convert your comma-delimited tags into an array, then loop through the array to do your inserts.
我将您的逗号分隔的标记转换为数组,然后循环遍历数组来执行插入。
$data_array=explode(",",$tag); // converts tags to an array based on the comma
// loop through each item in the array
foreach($data_array as $one_tag){
// Get rid of the space you have in your tag string
$one_tag=trim($one_tag);
// Do whatever sanitization you need to do to your data, for example...
$one_tag=mysql_real_escape_string($one_tag);
// Insert into the database
mysql_query("INSERT INTO table SET tag='$one_tag'");
}
You can make this more efficient using Prepared Statements, but it kept this example simple.
您可以使用准备语句来提高效率,但它使这个示例保持简单。
#5
0
This will work out according to your result
这将根据你的结果而定
$tagData = array();
$tagList = explode(",","tag1,tag2,tag3,tag4");
foreach ($tagList as $key=>$value) {
$tagData[] = '("' . $key . '", "' . $tagList[$key] . '")';
}
$query = 'INSERT INTO tags (id,tag) VALUES' . implode(',', $tagData);
echo $query;die;
mysql_query($query);
#6
0
$id=33;
$tag=$_GET['tag'];
$tag_Split= explode(",", $tag);
$cnt=count($tag_Split);
for($i=0;$i<$cnt;$i++)
{
mysql_query("insert into tag_table(id,tag)
values ($id, $tag_Split[$i])");
}
#7
-1
Try this for size: (combining ideas from a number of people who almost got it right).
试试这个尺寸:(把一些人的想法和他们的想法结合起来)。
$values = array(
33=>'tag1'),
33=>'tag2'),
33=>'tag3'),
);
$sql = "INSERT INTO table (id, value) VALUES ";
$count=0;
foreach ($values as $id=> $value)
{
if ($count++ > 0)
$sql .= ',';
$sql .= "($id, '$value')";
}
$query = mysql_query($sql, $db);
#1
3
Hi greetings from UK :)
Can't you just loop through the querystring fields and add one row to the database at a time?
嗨,来自英国的问候语:)难道你不能通过querystring字段循环一次向数据库添加一行吗?
e.g.
如。
<?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>';
}
?>
I uploaded this to http://cyba.co/test.php so you can see the output.
我上传了这个到http://cyba.co/test。可以看到输出。
#2
1
You can do an INSERT INTO
with multiple values:
可以用多个值插入:
INSERT INTO table (tag) VALUES ('tag1', 'tag2', 'tag3');
Make sure, you properly escape your input (if user suplied) to prevent SQL injection. You can use mysql_escape_string()
for this.
确保正确地转义输入(如果用户支持)以防止SQL注入。您可以为此使用mysql_escape_string()。
Edit: To get these values, you can do:
编辑:为了得到这些值,你可以这样做:
$values = explode(",", $_GET['your_param']);
foreach($values as $idx => $val) {
$values[$idx] = mysql_real_escape_string(trim($val));
}
mysql_query("INSERT INTO table (tag) VALUES (".implode(",", $values).");");
#3
0
Use explode()
function to split the string
函数的作用是:分割字符串
$tag = "tag1, tag2, tag3";
$pieces = explode(", ", $tag);
SQL query would be
SQL查询将
INSERT INTO myTable VALUES ($pieces[0],$pieces[1],$pieces[2]);
Good Luck!!!
祝你好运! ! !
#4
0
I would convert your comma-delimited tags into an array, then loop through the array to do your inserts.
我将您的逗号分隔的标记转换为数组,然后循环遍历数组来执行插入。
$data_array=explode(",",$tag); // converts tags to an array based on the comma
// loop through each item in the array
foreach($data_array as $one_tag){
// Get rid of the space you have in your tag string
$one_tag=trim($one_tag);
// Do whatever sanitization you need to do to your data, for example...
$one_tag=mysql_real_escape_string($one_tag);
// Insert into the database
mysql_query("INSERT INTO table SET tag='$one_tag'");
}
You can make this more efficient using Prepared Statements, but it kept this example simple.
您可以使用准备语句来提高效率,但它使这个示例保持简单。
#5
0
This will work out according to your result
这将根据你的结果而定
$tagData = array();
$tagList = explode(",","tag1,tag2,tag3,tag4");
foreach ($tagList as $key=>$value) {
$tagData[] = '("' . $key . '", "' . $tagList[$key] . '")';
}
$query = 'INSERT INTO tags (id,tag) VALUES' . implode(',', $tagData);
echo $query;die;
mysql_query($query);
#6
0
$id=33;
$tag=$_GET['tag'];
$tag_Split= explode(",", $tag);
$cnt=count($tag_Split);
for($i=0;$i<$cnt;$i++)
{
mysql_query("insert into tag_table(id,tag)
values ($id, $tag_Split[$i])");
}
#7
-1
Try this for size: (combining ideas from a number of people who almost got it right).
试试这个尺寸:(把一些人的想法和他们的想法结合起来)。
$values = array(
33=>'tag1'),
33=>'tag2'),
33=>'tag3'),
);
$sql = "INSERT INTO table (id, value) VALUES ";
$count=0;
foreach ($values as $id=> $value)
{
if ($count++ > 0)
$sql .= ',';
$sql .= "($id, '$value')";
}
$query = mysql_query($sql, $db);