Insert multiple rows into mysql (items separated by comma)

前端 未结 7 581
执笔经年
执笔经年 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:18

    Try this for size: (combining ideas from a number of people who almost got it right).

    $values =  array(
                        33=>'tag1'),
                        33=>'tag2'),
                        33=>'tag3'),
                    );
    
    $sql = "INSERT INTO table (id, value) VALUES ";
    $count=0;
    foreach ($values as $id=> $value)
    {
        if ($count++ > 0)
            $sql .= ',';
        $sql .= "($id, '$value')";
    }
    $query = mysql_query($sql, $db);
    

提交回复
热议问题