Hi.
This is actually quite simple to do once you get used to using arrays.
... Which is true of most things I guess :)
In this case, you can simply split your string into pieces using the
explode function. This will create an array, each element containing one piece.
Then you could either use a
foreach loop loop to insert each piece into your database.
Or, my personal preference, use the
implode function to re-assemble the pieces into a VALUE clause for a single INSERT query.
Consider this:
-
<?php
-
// This is the string, delimited by a comma, rather than a new-line.
-
$rawString = "piece1,piece2,piece3";
-
-
// Split the string into pieces
-
$pieces = explode(",", $rawString);
-
-
// Build the top of the INSERT query
-
$sql = "INSERT INTO `myTable`(`myField`) VALUES\n";
-
-
// Build the rest of the INSERT query by re-assembling the
-
// pieces.
-
$sql .= "('";
-
$sql .= implode("'), ('", $pieces);
-
$sql .= "')";
-
?>
-
Now the $sql variable should be somewhat like:
-
INSERT INTO `myTable`(`myField`) VALUES
-
('piece1'), ('piece2'), ('piece3')
-
Be careful tho if this data is coming from a client.
Make sure you run it through the
mysql_real_escape_string function first, and perhaps even the
trim function to.