Multiple mySQL insert using textarea

Question:

I am trying to create a form where I can cut an paste records in a textarea and submits each record as new row in mysql.

Example:

I have records such like this.

—-
Toyota
Honda
BMW
—-

I would like to copy the text and paste it in a textarea then hit submit.  The solution I am looking for is an example of the mySQL statement or PHP to take the data and insert as multiple rows.

My guess is inserting in mysql rows from a textarea separated by a carriage return?

Solution:

@jaxstorm – you cannot use explode, in my tests it failed.  I had to use preg_split.

However, your second portion is correct, although I am not sure why you wouldn’t just use foreach for looping through an array and assigning values.

if($_POST['txt'])
{
$sampstring = rtrim($_POST['txt']);
$sampstring = preg_split(“/\r\n/”,$sampstring); // remember to try variations of just \r and \n
}
foreach ($sampstring as $value) {
$query = “INSERT INTO database_name (Column_name)
VALUES (‘$value’)”;
mysql_query($query) or die(mysql_error())
}

Từ khóa:
digg delicious stumbleupon technorati Google live facebook Sphinn Mixx newsvine reddit yahoomyweb