Jump to content

nl2br not working?


SharkBait

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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));

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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;
}

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.