0

Basically when i insert textarea value into my mysql database, the new lines are not preserved.

At this point people will say use nl2br(). But that is a solution for people that can't get the new lines to display in html. This is not my problem.

I know this because i changed the column value and added newlines directly in phpmyadmin, and they all showed up in rendered html.

Somehow between the Ajax request > Post request > PDO Query Insert > PhpMyAdmin, the new lines are removed.

Thanks.

atymic
  • 3,093
  • 1
  • 13
  • 26
  • 1
    Are you sure they are removed or you just can't see them in phpMyAdmin? – Dharman Aug 01 '19 at 21:51
  • 1
    Possible duplicate of [How can I prevent SQL injection in PHP?](https://stackoverflow.com/q/60174/1839439) – Dharman Aug 01 '19 at 21:53
  • @Dharman is most likely right PHPMyAdmin is a webapplication, browsers do not show linebreaks in formats as `\n` or `\n\r` – Raymond Nijland Aug 01 '19 at 21:53
  • phpMyAdmin is not a database, it's a front-end to MySQL which is a database. Whatever you insert through phpMyAdmin should be fine. **It's the responsibility of your application to properly escape it for the final display context**. You don't use `nl2br()` when inserting, you use it when displaying. – tadman Aug 01 '19 at 22:08
  • Please read How to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question accordingly. You need to provide us with your attempt, example data, expected results and what results you're currently getting – atymic Aug 01 '19 at 22:12
  • Do you know how to examine the string at each point in the process? – Don't Panic Aug 01 '19 at 22:30

1 Answers1

0

I found a semi solution.

Basically the new lines were getting removed in the ajax get request, before being sent to my php script and inserted to db.

The solution is to do a post request with jQuery. Unfortunetly I could not find a vanilla js solution.

 $.post(
   '../scripts/saveNote.php', 
   {
     note: textarea.val()
   },
   function (ajaxResponse) {
     if ( ajaxResponse == "success" ) {
       // Process success             
     }
   }
 );