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

时间:2012-09-19 20:44:22

标签: php mysql arrays explode

我有tag1,tag2,tag3形式的标签,我想输入一个表格,即

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))."')";
       } 

1 个答案:

答案 0 :(得分:2)

考虑将PDO用于SQL请求: http://php.net/manual/en/book.pdo.php

例如:

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