douyong1850
2014-05-03 05:00
浏览 73
已采纳

将PHP数组中的值插入到mySQL行中

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.

图片转代码服务由CSDN问答提供 功能建议

我将逗号分隔列表爆炸成名为 $ tagArray 的变量并尝试 将这些值写入表的递增行。

下面的代码可以在我的表中创建适当数量的行,但它只写下逗号分隔的第一个字母 string(即如果字符串读作“one,two,three”,“o”写在第一行,其余两行的列为“blog_tag”的空白值: < pre> $ 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)); }

这是意大利面条拼凑的tog 来自各种搜索的以太它已经接近但我无法弄清楚它只抓住字符串的第一个字母。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题