-2

Possible Duplicate:
Add new row in MYSQL from Comma Separated text in textbox
Insert comma separated records in table

$words = 'word1,word2,word3';

How do i get this data into following structure:

MySQL table:

id    value
 1    word1
 2    word2
 3    word3

What i am thinking of:

$separated = explode(",",$words);
//do some kind of loop
INSERT INTO table (value) VALUES ('$something');

I guess i suck at loops :/

Thanks

Community
  • 1
  • 1
kristjanzzz
  • 69
  • 1
  • 6
  • 3
    If you know you need a loop, write one and see what happens. If it doesn't work show us what you've got and we can help you with it. But *at least try*. – John Conde Jan 28 '13 at 02:01
  • 2
    do the freakin loop. If you `suck at loops` open freakin manual at loop page, read, use it and you gonna be loop master – Peter Jan 28 '13 at 02:01
  • 1
    Seems like a duplicate: http://stackoverflow.com/a/8135264/1073631 – sgeddes Jan 28 '13 at 02:06

2 Answers2

2
$wordArray = explode(",", $words);

$stmt = "INSERT INTO table (column) VALUES ('" . implode("'), ('", $wordArray) . "')";

This will produce:

INSERT INTO table (column) VALUES ('word1'), ('word2'), ('word3')
Kermit
  • 33,827
  • 13
  • 85
  • 121
0

Do a simple loop, as such:

foreach($separated as $seperate){
    $sql .= "INSERT INTO table (value) VALUES ('$seperate')";
}
boruch
  • 453
  • 3
  • 16