我将逗号分隔列表爆炸成名为 下面的代码可以在我的表中创建适当数量的行,但它只写下逗号分隔的第一个字母 string(即如果字符串读作“one,two,three”,“o”写在第一行,其余两行的列为“blog_tag”的空白值: p>
< pre> 这是意大利面条拼凑的tog 来自各种搜索的以太它已经接近但我无法弄清楚它只抓住字符串的第一个字母。 p>
div> $ tagArray code>的变量并尝试 将这些值写入表的递增行。 p>
$ tagInput = $ _POST [“blog_tags”];
$ tagArray = explode(“,”,$ tagInput);
$ sql_2 =“INSERT INTO blog_tags(blog_tag,blog_id)
VALUES” ;
$ valuesArray = array();
foreach($ tagArray as $ row){
$ tag = mysqli_real_escape_string($ conxn,$ row ['blog_tag']);
$ valuesArray [] =“( '$ tag','$ lastID')“;
}
$ sql_2。= implode(',',$ valuesArray);
if(!mysqli_query($ conxn,$ sql_2)){\ ndie('错误:'。mysqli_error($ conxn));
}
code> pre>
I'm exploding a comma separated list into a variable named $tagArray
and trying to write those values into incrementing rows of a table.
The code below works in so much as it creates the appropriate number of rows in my table but it only writes the first letter of the comma separating string (ie. if the string reads as "one, two, three", "o" is written in the first row and the remaining two rows have a blank value for column "blog_tag":
$tagInput = $_POST["blog_tags"];
$tagArray = explode(",",$tagInput);
$sql_2 = "INSERT INTO blog_tags (blog_tag, blog_id)
VALUES ";
$valuesArray = array();
foreach($tagArray as $row){
$tag = mysqli_real_escape_string($conxn, $row['blog_tag']);
$valuesArray[] = "('$tag','$lastID')";
}
$sql_2 .= implode(',', $valuesArray);
if (!mysqli_query($conxn,$sql_2)) {
die('Error: ' . mysqli_error($conxn));
}
This is spaghetti pieced together from various searches here and it's getting close but I can't figure out where it's grabbing just that first letter of the string.