JoeMorgan
JoeMorgan

Reputation: 143

HTML Table not recognising line breaks from mysql database

I have a mysql database with an 'address' field (VARCHAR). I also have a html table that is used to display the addresses of different staff members.

My problem is that data is stored in the address field of the database with linebreaks between each line of the address, but when the data is displayed in the table the line breaks are not there, so the entire address is displayed on one line. How can I fix this?

I'm sure this is a question that is asked all the time, but I can't find the answer anywhere. I'm new to PHP so please forgive my naivety.

UPDATE:

This is basically my code:

if(isset($_POST['submit'])) {

$address  = $_POST['address'];

$queryupdate = "UPDATE Staff SET 
      address= :address WHERE id= :id";
$q = $db->prepare($queryupdate);
$q->execute(array(
        ":id"    => $id,
        ":address"  => $address));

The data in the $address variable is taken from a simple textarea.

Upvotes: 2

Views: 2337

Answers (2)

JoeMorgan
JoeMorgan

Reputation: 143

Using help from @Boy :

I added 'nl2br' before the output of my string.

Upvotes: 0

user771071
user771071

Reputation:

Actual line breaks are never shown in HTML unless the word-wrap is set to pre, or an actual pre tag is used. However, to overcome this you can use the nl2br() functionality in PHP. What you'll need to do is use the nl2br() before outputting your data to the browser, and it will give you a HTML formatted string back where the line breaks are prepended by <br> tags.

See the documentation about nl2br() here: http://php.net/manual/en/function.nl2br.php

Upvotes: 4

Related Questions