Php 拆分字符串并将每个值输入mysql

Php 拆分字符串并将每个值输入mysql,php,mysql,arrays,explode,Php,Mysql,Arrays,Explode,我有tag1、tag2、tag3形式的标签,我想将它们输入到a表中,即 tag_id tag_name topic_id 1 php 1 2 ajax 2 3 mysql 3 4 html 4 下面的脚本没有这样做。我没有在屏幕上看到任何错误 $topic_tag = explode("," , $topic_tags) ;

我有
tag1、tag2、tag3形式的标签,我想将它们输入到a表中,即

tag_id    tag_name    topic_id
  1         php          1
  2         ajax         2
  3         mysql        3
  4         html         4
下面的脚本没有这样做。我没有在屏幕上看到任何错误

$topic_tag = explode("," , $topic_tags) ;

       foreach($topic_tag as $topic_tags) {
       $query = "INSERT INTO `forum_tags` (`tag_id`, `tag_name`) 
       VALUES ( NULL, '" .mysql_real_escape_string(trim($topic_tags))."')";
       } 

考虑对SQL请求使用PDO:

例如:

$statement = $pdo_object->prepare('INSERT INTO `forum_tags` (`tag_id`, `tag_name`, `topic_id` ) VALUES ( ?, ?, ?) ');
$statement->execute( $topic_tags );

您是否在任何地方执行查询?您正在将其分配给变量,但我没有看到
$mysqli->query($query)键入call anywhere…那么主题id呢?是否定义为非空?您计划在哪里设置此值?啊,停止重用$topic_标记和$topic_标记!与直觉相反的是。。。