Insert multiple comma or linebreak seperated values from textarea into MySQL

Posted on 2006-05-30
Last Modified: 2008-02-26
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.

<textarea name="emails">

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.
Question by:kenitech
    LVL 7

    Expert Comment

    It would be good to know what programming language you are using for the database interaction. If you are using ASP, you can use the <b>Join() </b> method of the string object to turn the textarea text into an array, then loop through the array as you indicated.

    LVL 9

    Expert Comment

    in ASP/VB(.net) this can be done with


    which would replace all CRLF with commas and ResultText will equal  

    <textarea name="emails">,,,</textare>

    Better Still..

    ResultTextArray as String()=split (OriginalText,vbcrlf)
    for tCount as integer = 1 to ResultTextArray.length

    Is that what you mean?

    Author Comment

    hey, i'm totally sorry. i'm using PHP.
    LVL 7

    Expert Comment


    I have to let a PHP person handle this one then.

    LVL 7

    Expert Comment

    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.


    Author Comment

    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] . "')");

    Accepted Solution

    PAQed with points refunded (125)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now