Insert multiple rows into mysql (items separated by comma)

前端 未结 7 639
执笔经年
执笔经年 2021-01-14 12:57

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[])

tag1, tag2, tag3, tag4

7条回答
  •  轮回少年
    2021-01-14 13:27

    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.

提交回复
热议问题