play_ Posted April 1, 2006 Share Posted April 1, 2006 Hi.I retrieve news from my database.and to print out, i use echo nl2br($row['news']).But it doesnt work.Then, i click "edit news" which takes me to a form with the news as the value in the text area.Now i see the news with <br> where i had pressed enter when i submitted.So all i have to do is click submit and itll update the database with the new news.however, that doesnt sound right. I shouldnt have to submit the news, then edit it for the nl2br to work.Does anyone know what's wrong? Quote Link to comment Share on other sites More sharing options...
Guest footballkid4 Posted April 1, 2006 Share Posted April 1, 2006 1) What OS are you running (yes, it does matter because *NIX uses: \n, Windows uses \r\n, and Mac uses \r)2) What code are you using to insert the information into the DB Quote Link to comment Share on other sites More sharing options...
play_ Posted April 1, 2006 Author Share Posted April 1, 2006 Hi and thanks!It's a RedHat Linux server.To insert, i have a form, and heres the php code for it:[code]<?phpif (isset($_POST['submit'])) { $news = escape_data($_POST['news']); $title = escape_data($_POST['title']); $query = "INSERT INTO news (title, news, date_submitted, author) VALUES ('$title', '".wordwrap($news, 45, "\n", 1)."', NOW(), '". $_SESSION['user_name'] ."')"; $result = mysql_query($query) or die (mysql_error()); if ($result) { header ('Location: index.php'); } else { echo 'Could not add news.'; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted April 1, 2006 Share Posted April 1, 2006 Don't store formatted news in the DB. Store it from the textarea complete with "\n"s.Format it when you output it. Quote Link to comment Share on other sites More sharing options...
play_ Posted April 1, 2006 Author Share Posted April 1, 2006 [!--quoteo(post=360583:date=Apr 1 2006, 05:26 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 1 2006, 05:26 AM) [snapback]360583[/snapback][/div][div class=\'quotemain\'][!--quotec--]Don't store formatted news in the DB. Store it from the textarea complete with "\n"s.Format it when you output it.[/quote]Hi Barand.Are you talking about this: wordwrap($news, 45, "\n", 1) ?Thats the only formatting i do when inserting news into the db (besides escaping quotes) Quote Link to comment Share on other sites More sharing options...
Barand Posted April 1, 2006 Share Posted April 1, 2006 If you are seing '<br>' in the text area when you pull it from the table then it sounds as though somewhere there is a nl2br($news) before it is saved to the table.Also, re wordwrap - if you subsequently redesign the page and want to wrap to a different width then you're going to have some "unwrapping" to do first. Quote Link to comment Share on other sites More sharing options...
play_ Posted April 1, 2006 Author Share Posted April 1, 2006 Oh, Barand i think i didn't explain very well the problem, sorry.here's exactly how it goes:- I type the news in a text area and click submit and it puts it in the database- I use am redirected to the index page, and notice that the news don't have any linebreaking at all. It's all crumbled up together.- Then i click "edit", and i am taken to the edit form, but now i see <br> where i had pressed enter when i first entered the news. So since the <br>'s are there when i go edit it, i just click "submit updated news".so it doenst show on the news page, but only when i edit.Hope this helps better.And i don't format it with nl2br before entering it in the db. here is the full code for "add news":[code]<?php$title = 'Post news';$header_title = 'Add news. . .';include ('./includes/header.php');if (!isset($_SESSION['user_name'])) { header ("Location: index.php");}?><form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td>Title:</td> </tr> <tr> <td><input type="text" name="title" /></td> </tr> <tr> <td></td> </tr> <tr> <td>News:</td> </tr> <tr> <td><textarea name="news" rows="7" cols="40"></textarea></td> </tr> <tr> <td><input type="submit" name="submit" value="post news" /></td> </tr> </table></form><?phpif (isset($_POST['submit'])) { require_once ('./db_connect.php'); $news = escape_data($_POST['news']); $title = escape_data($_POST['title']); $query = "INSERT INTO news (title, news, date_submitted, author) VALUES ('$title', '".wordwrap($news, 45, "\n", 1)."', NOW(), '". $_SESSION['user_name'] ."')"; $result = mysql_query($query) or die (mysql_error()); if ($result) { header ('Location: index.php'); } else { echo 'Could not add news.'; }}include ('./includes/footer.php');?>[/code]Escape data, goes as follows:[code] function escape_data($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysql_real_escape_string(trim($data), $dbc); }[/code]Ive also tried inserting it without wordwrap, to see if it was the problem, and it wasn't. same thing happens Quote Link to comment Share on other sites More sharing options...
graecyn Posted April 2, 2006 Share Posted April 2, 2006 Nevermind!!! Covered already. :P Quote Link to comment Share on other sites More sharing options...
Barand Posted April 2, 2006 Share Posted April 2, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]And i don't format it with nl2br before entering it in the db. here is the full code for "add news":[/quote]In that case, are you using nl2br() when you put it into textarea for editing? Quote Link to comment Share on other sites More sharing options...
play_ Posted April 5, 2006 Author Share Posted April 5, 2006 [!--quoteo(post=360844:date=Apr 2 2006, 05:44 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 2 2006, 05:44 AM) [snapback]360844[/snapback][/div][div class=\'quotemain\'][!--quotec--]In that case, are you using nl2br() when you put it into textarea for editing?[/quote]Yes.I suppose i should take that out huh? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 5, 2006 Share Posted April 5, 2006 You guessed right Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.