Seperating info from a text field and inserting into db using foreach????

The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Seperating info from a text field and inserting into db using foreach????

Hi I am trying to figure out a way to take email address out of a text field box and insert them as new records into a database.

I created a page with a text field and a add button.

As an example, I want to be able to paste more than one email address into the text field box. The email addresses will be seperated by commas like this (user123@blah.com, user234@blah.com, user345@blah.com, user456@blah.com and so on.) When I press the add button I want to be able to extract the emails and add them into the database as thier own entries as new email adresses. I was trying to configure a script using foreach but I can not figure out exactly how to do this.
I do not know how to extract each email individually from the text field and how to list them as a variable and enter them into the db...

$array = explode( ',', $fielddata);
foreach ( $array as $v ) {
// remove the whitespace
$v2 = trim( $v );
// You can also add other checks, for example using a regex
// you could check if the address is in the correct format
// insert into DB here
}

use the explode function (see first line in clickhere's code) it turns a sring in to an array. You first specify which character you want to split the string on ( a comma in this case) then which string you want to explode.

You'll just need to get your string in to the $fielddata variable then you'll be away with the code above.

$array = explode( ',', $fielddata);
foreach ( $array as $v ) {
// remove the whitespace
$v2 = trim( $v );
// You can also add other checks, for example using a regex
// you could check if the address is in the correct format
// insert into DB here
}

and use mysql_real_escape_string( ) instead of addslashes()

Ok which variable would i use to specify the email when entering into the db? I am confused. $v??

use the explode function (see first line in clickhere's code) it turns a sring in to an array. You first specify which character you want to split the string on ( a comma in this case) then which string you want to explode.

You'll just need to get your string in to the $fielddata variable then you'll be away with the code above.