Search code examples
phpmysqlnl2br

PHP nl2br() to new MYSQL row


I have one text area where user can type in multiple lines. I would like to make it that when a new line is entered, it gets inserted as a new record in my table. How can I use the nl2br() function to accomplish this? Is there an easy solution I'm overlooking or do I need something other than nl2br?


Solution

  • nl2br() is only doing a replace of "\n" to "<br>" on whatever variable you pass. It's not breaking the variable into multiple variables/array.

    You would need to do something like

    $lines = explode("\n", $_POST['text_area_input']);
    foreach($lines as $line) {
        // do database insert query for each $line
    }
    

    Of course you should also be sanitizing the user input