kenitech
asked on
Insert multiple comma or linebreak seperated values from textarea into MySQL
I have a mailing tool that I want to allow users to paste in multiple email addresses.
For arguments sake, let's say the are seperated by linebreaks.
example:
<textarea name="emails">
email1@host.com
email2@host.com
email3@host.com
</textare>
On submit I would like the emails to be added one by one in a loop. Each name is a new row in my table and the email is added to the field 'email'. I'm assuming the values need to be put into an array, and then for the number of values in the array (x), the sequel will run x times. I just don't know the exact syntax.
For arguments sake, let's say the are seperated by linebreaks.
example:
<textarea name="emails">
email1@host.com
email2@host.com
email3@host.com
</textare>
On submit I would like the emails to be added one by one in a loop. Each name is a new row in my table and the email is added to the field 'email'. I'm assuming the values need to be put into an array, and then for the number of values in the array (x), the sequel will run x times. I just don't know the exact syntax.
in ASP/VB(.net) this can be done with
ResultText=Replace(Origina lText,vbCR LF,",")
which would replace all CRLF with commas and ResultText will equal
<textarea name="emails">email1@host. com,email2 @host.com, email3@hos t.com,</te xtare>
Better Still..
ResultTextArray as String()=split (OriginalText,vbcrlf)
for tCount as integer = 1 to ResultTextArray.length
DoWhateveroperationYouAreT ryToDo(Res ultextArra y(tCount))
next
Is that what you mean?
ResultText=Replace(Origina
which would replace all CRLF with commas and ResultText will equal
<textarea name="emails">email1@host.
Better Still..
ResultTextArray as String()=split (OriginalText,vbcrlf)
for tCount as integer = 1 to ResultTextArray.length
DoWhateveroperationYouAreT
next
Is that what you mean?
ASKER
hey, i'm totally sorry. i'm using PHP.
hmmm...
I have to let a PHP person handle this one then.
M@
I have to let a PHP person handle this one then.
M@
It looks like you could use a preg_split function in PHP to get the array, then use a for...each contruct to insert each element.
I don't use PHP much so this is based on some research and I am sure of the exact syntax.
M@
I don't use PHP much so this is based on some research and I am sure of the exact syntax.
M@
ASKER
hey, i just solved this one.
here's a solution that seems to work.
do i close this question??
i probably should've posted it in PHP in the first place..
//Characters to delimit by
$patterns[0] = '/,/';
$patterns[1] = '/:/';
$patterns[2] = '/\n/';
//the form post variable
$emails_added = $_POST["add_mail"];
//split out the values on an array bases on the above
$emails_split = explode(',' , preg_replace($patterns,',' , $emails_added));
//loop through insert based on number of values in array
for ($i=0; $i<count($emails_split); ) {
$db->execute("insert into my_mailer (email) values ('" . $emails_split[$i] . "')");
$i++;
}
here's a solution that seems to work.
do i close this question??
i probably should've posted it in PHP in the first place..
//Characters to delimit by
$patterns[0] = '/,/';
$patterns[1] = '/:/';
$patterns[2] = '/\n/';
//the form post variable
$emails_added = $_POST["add_mail"];
//split out the values on an array bases on the above
$emails_split = explode(',' , preg_replace($patterns,','
//loop through insert based on number of values in array
for ($i=0; $i<count($emails_split); ) {
$db->execute("insert into my_mailer (email) values ('" . $emails_split[$i] . "')");
$i++;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
M@