PHP - Use one field to create multiple database row entries
I am trying to work out the best way of using one form field that can update multiple rows i开发者_JAVA百科n mysql. My intention is to capture a set of web addresses in one box om a form e.g.
http://google.com http://www.bing.com http://www.yahoo.com https://wwwpaypal.com
And I would like that to create four entries in my database:
http://google.com
http://bing.com
http://www.yahoo.com
https://wwwpaypal.com
Is the best way to try and find the spaces and split the post input or is there a better way?
Thanks
Split on the spaces in PHP and just loop to insert them into the database.
$entries = explode(' ', $list_of_entries);
foreach ($entries as $entry) {
// Assuming you need some id column to map entries in `$id_column_value`...
$entry = mysql_real_escape_string($entry);
mysql_query("INSERT INTO table (col1, col2) VALUES('$id_column_value', '$entry');
}
$entries = explode(" ", $_POST['entries']);
foreach ($entries as $entry){
mysql_query("INSERT INTO entries ('entry') VALUES ('".mysql_real_escape_string($entry)."');");
}
Yep, I would just use
$arrayOfAddresss = explode(" ",$formInput);
and insert these into the table.
精彩评论