SharkBait Posted January 5, 2009 Share Posted January 5, 2009 Ok this has me stumped and I am sure someone can help me out When someone uses a textarea to insert multiple lined input it gets store into the database and has the \n added to where the new lines are to occur. When i read in the information from the database I use nl2br() but it doesn't seem to be adding the <br /> where the \n are but just displays the \n. Why is this? Am I missing something?? It's got me stumped on one of my projects and I don't know why this is happening. If the user hits enter/return multiple times in the textarea it just stores the \n\n etc but when I pull from the database and use nl2br() it just displays the \n\n. Example: <?php $value = "this is fun\n\n right? \n\n woo fun times ahead!"; ?> What I wrote: <?php echo nl2br($value);?> I leave the \n in the table's value because it can be read back into a textarea to be edited again by the user at a later time. I also noticed when I read it back into a textarea it shows up as \n and does not perform the actual line break. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 How are you escaping the data going into the database? It sounds like you are adding too many slashes and thus the \n is being escaped so it displays it instead of parsing it. Just a question though, have you tried running the example you posted? Meaning bypassing the database? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 you need to use mysql_real_escape_string(); Make sure your not using addslashes. code works. What I wrote: this is fun<br /> <br /> right? <br /> <br /> woo fun times ahead! Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 I've been using mysql_real_escape_string(trim($value)) when inserting into the database. I just tried it sans the DB and it works as expected. Why is this happening all the sudden with mysql_real_escape_string() where I don't think it has ever done that before? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 when the user hit's a enter button you get ^ then the nl2br() converts ^ to a <br /> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 5, 2009 Share Posted January 5, 2009 Is magic_quotes turned on? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 Check if get_magic_quotes_gpc is on. If it is then I would create a new function like so: <?php function myEscape($string) { return (get_magic_quotes_gpc())?mysql_real_escape_string(stripslashes($string)):mysql_real_escape_string($string); } ?> And use that instead, this way if your script ever goes onto another server you do not have to worry if magic_quotes is on or off. Then you would change the following: mysql_real_escape_string(trim($value)) To $value = myEscape(trim($value)); Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 Is magic_quotes turned on? magic_quotes_gpc = Off magic_quotes_runtime = Off Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 Is magic_quotes turned on? magic_quotes_gpc = Off magic_quotes_runtime = Off When you look at the data in the database, does it have an extra slash in it before the \n ? If it does, magic_quotes was turned on at one point and doubled up the quotes, which is why you are having this issue. Have you tried entering in a new post and see if that doubles it up or if it displays right? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 brilliant function for commercial scripts or gpl codes. but if the code is for you only, consider turning magic_quotes off. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 The database does not have any extra slashes added. We have Part Numbers...We should use them!\n\nThis will be of great assistance during inventory times, as well as avoiding any confusion caused by referring to items by their model name. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 Is addslashes on. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 Is addslashes on. Nothing is using addslashes() Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 bet it this. Note: Note that when magic_quotes_sybase is ON it completely overrides magic_quotes_gpc . In this case even when magic_quotes_gpc is enabled neither double quotes, backslashes or NUL's will be escaped. magic_quotes_sybase "0" PHP_INI_ALL Removed in PHP 6.0.0. http://uk.php.net/manual/en/sybase.configuration.php#ini.magic-quotes-sybase Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 magic_quotes_sybase = Off Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 post your code please it you then. have you tried the function as provided good code practice. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 I just find it odd that nl2br() won't add the <br /> from the information coming from the database.... none of the \n are double escaped in the raw data when I look at the row via MySQL on the command line. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 Here is the script that just shows the row the user has specified: <?php $ID = $_GET['id']; $str = "SELECT * FROM main_eco_data WHERE id = '{$ID}'"; $qry = $MySQL->DoQuery($str, $DBLINK); $results = $MySQL->FetchArray($qry); ?> <div style="line-height: 25px;"> <a href="javascript:history.back(-1)">Go Back</a><br /> ECO #: <?php echo $results['id'];?><br /> Title: <?php echo $results['title'];?><br /> Reason: <?php echo nl2br($results['reason']);?><br /> Originiator: <?php echo $results['originator'];?><br /> Cost: <?php echo $results['cost'];?><br /> Implimentation Date: <?php echo date('M d, Y', strtotime($results['implimentation_date']));?><br /> <br /> Summary:<br /> <?php echo $results['summary'];?> </div> $MySQL is a custom class I created that doesn't do any fancy formatting, just made it easier for me to not have to type out the function names all the time Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 So in the above code $result['reason'] would be where the \n are and the nl2br() is not doing its job Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 5, 2009 Share Posted January 5, 2009 insert code not the ouput, oh my god, Please post the insert class you maid please to insert the database entry's. Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 5, 2009 Author Share Posted January 5, 2009 insert code <?php if(isset($_POST['submit'])) { $errField = array(); foreach($_POST as $field => $value) { if($field != "submit" ) { switch ($field) { // any special dealings? case 'id': if(!isset($_GET['id'])) { // New entry skip id field } break; case 'reason': case 'summary': if(!empty($_POST[$field])) { $ECO[$field] = mysql_real_escape_string(trim($value)); } else { $errField[$field] = 1; } break; default: if(!empty($_POST[$field])) { $ECO[$field] = mysql_real_escape_string(trim($value)); } else { $errField[$field] = 1; } break; } } } // showPOST($ECO); // exit(); if(count($errField) > 0) { $errMsg = "<div class=\"error\">(". count($errField) .") Errors were found, please ensure all fields are filled out properly.</div><br />\n"; } else { // Submission sucessful - INSERT into DB if(isset($_GET['action']) && $_GET['action'] == "edit") { $str = "UPDATE main_eco_data SET id = '{$ECO['id']}', title = '{$ECO['title']}', reason = '{$ECO['reason']}', cost ='{$ECO['cost']}', ". "Expires_On='{$ECO['Expires_On']}', implimentation_date = '{$ECO['implimentation_date']}', summary = '{$ECO['summary']}', PartNumber = '{$ECO['partnumber']}', date_last_modified = NOW() WHERE id = '{$ID}'"; } else { $str = "INSERT INTO main_eco_data (id, title, originator, reason, cost, PartNumber, implimentation_date, summary) ". "VALUES (NULL, '{$ECO['title']}', '{$_SESSION['ecos']['username']}', '{$ECO['reason']}', '{$ECO['cost']}', '{$ECO[partnumber]}', '{$ECO['implimentation_date']}', '{$ECO['summary']}')"; } //echo "<p>{$str}</p>"; $qry = $MySQL->DoQuery($str, $DBLINK); header("Location: {$_SERVER['PHP_SELF']}"); exit(); } } ?> My custom MySQL class <?php class MySQL { var $link; var $query; var $num; var $results = array(); function MySQL($host, $username, $password, $database) { $this->host = $host; $this->username = $username; $this->password = $password; $this->database = $database; } function Connect() { // Connect to MYSQL DB with defined settings from config.php $this->link = mysql_connect($this->host, $this->username, $this->password, true) or die("MySQL Cannot Connect: ". mysql_error()); // Select database to work with mysql_select_db($this->database, $this->link) or die("MySQL Cannot Select {$this->database}: <br />". mysql_error()); // Return the link to be used later return $this->link; } function Disconnect($link) { // Close an active MySQL Connection $this->link = $link; mysql_close($this->link); } function DoQuery($query, $link) { // Basic Query Execution $this->query = $query; $this->link = $link; //echo "LINK: {$this->link}<br />"; $this->query = mysql_query($this->query, $this->link) or die("<strong>MySQL DoQuery Error:</strong><span style=\"color: #f00;\"> <br />{$query} <br />". mysql_error($this->link). "</span>"); return $this->query; } function FetchArray($query) { // Retrieve results of DoQuery $this->results = mysql_fetch_array($query, MYSQL_ASSOC); return $this->results; } function NumRows($query) { // get Number of results $this->num = mysql_num_rows($query); return $this->num; } } ?> Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 6, 2009 Author Share Posted January 6, 2009 Hrmm still trying to figure this one out. 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.