HTML Table not recognising line breaks from mysql database - php

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.

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

Using help from #Boy :
I added 'nl2br' before the output of my string.

Related

My PHP Variable that I pulled from a MySQL database keeps returning undefined

Hope all is well.
I have had a bit of an issue this evening when I was working on a message system for some project for work.
I'm working on the inbox, and have it set up so that it pulls the message subject, sender, and date of sending in table rows on the front-end. That all works great.
My issue is pulling the message body, which is the actual message content. It's supposed to show up in a modal when the table rows are clicked in the front-end, and I can get that to work without issue. The problem is that I can't pull a non-undefined variable for the message content in the first place.
My SQL table looks like this:
msgTo (text), msgFrom (text), msgSubject (text), msgMessage (text), msgDate (text)
My PHP code looks like this:
$msgTo = $row["msgTo"];
$msgFrom = $row["msgFrom"];
$msgSubject = $row["msgSubject"];
$msgTime = $row["msgTime"];
$msgDate = $row["msgDate"];
$msg = $row["msgMessage"];
If I echo or print any of the variables other than my $msg variable, it works great. But no matter what I try, my $msg variable returns undefined.
The content for the "msgMessage" column in the MySQL table is the following:
"Hello John,
How's it going?"
My best guess is that because unlike all the other variables I'm pulling, this one has line breaks, and maybe it can't handle the equivalent of "\n"? So maybe there is some sort of way I need to sanitize it.
Please let me know if there is more information you need.
SOLUTION
My query did not include the variable I was trying to retrieve. It was just one of those silly mistakes that you end up spending too much time on.
If you're just now finding this post, remember to double-check your queries, everyone!
Your problem is here. Mask single quote
"Hello John,
How\'s it going?"
for example
$msg = str_replace("'", "\'", $row["msgMessage"]);

Passing LONG queries to MySQLI through mysqli_query()

so here's what I want to do, I want to be able to cache a whole web page, which would be something like this
$domain = "cnn.com";
$title = "CNN";
$cacheado = file_get_contents('http://www.google.com');
$ingresar = "INSERT INTO indexed_links (link, title, cacheado) VALUES ('$domain', '$title', '$cacheado')";
$db_on = mysqli_connect('localhost', 'root', 'pass', 'data_base');
mysqli_query($db_on, $ingresar);
My great concern is, I tried to do this exact code with the page example.com, which would be $cacheado = file_get_contents('http://example.com');
Which worked completely fine, it added the whole HTML code to the page, which is something somewhat short, it's an easy HTML code, now, Google and a bunch of other sites got more codes into their HTML, as a result, sites with longer HTML codes are not going through the mysqli_query query, which I suppose it has to do with MySql and not PHP because the code works just fine with example.com...
The column of the table which I want to insert the HTML code is cacheado, which has a type set in the MySQL database of text, does this have something to do?
The problem is not with the datatype. Instead, you failed to escape the strings before building the INSERT statement.
In particular, there was probably an apostrophe (') in that web page.
Change field type from TEXT() to VARCHAR(65535). It is the max lenght I now you can set for a field.

Hindi font in posted data not getting saved

I am posting Comments in hindi to another page, where i insert to db, but the comments is getting saved as junk in mysql.
--PHP
<form method='post' action='post.php'>
<textarea name='comments'></textarea>
</form>
--post.php
$comment =$_POST["comments"];
$query = "INSERT INTO comments SET comment='".mysql_escape_string($comment)."' ";
mysql_query($query) or die(mysql_error());
In DB the text gets inserted as junk characters
the database is of correct type as i have inserted the html notation on हिंदी { hindi characters to be saved} , and display works with correct META type,
but DB entry is like à╧√à╧¿à╧?à╧▌à╛?
i tried an insert query in the same file and it worked, but posting to some other file creates the problem, is it someting to do with posted data ?
i cannot use same file for insertion as other forms are there in the same page, which are handled.
You should try setting your "file encoding" to UTF-8 and so does your database encoding.
Most people forget that a file also has an encoding type. Especially on Windows this will bring you trouble.

How to extract text from MySQL database with carriage returns?

I have given input into database from textarea by MySQL.In that textarea i had typed carriage return few times. Whenever i am trying to extract that data from database, a simple line is shown how can i get the data or text as i given input with the carriage returns?
$query2 = new Bin_Query();
$sql2="select * from `product_table` where product_id='".$_GET['product_id']."'";
if($query2->executeQuery($sql2))
$product_records = $query2->records;
$output['product_details']=$product_records[0]['product_details'];
that's how i retrieved the data.
You have to use nl2br(), check out this about more http://php.net/manual/en/function.nl2br.php
actually, the correct way to use nl2br() is not to use it at all when you store your data - it's when you read from the database and are about to output it to the client. Data should not be formatted when inserted to the database, what if you later on want to create a REST-service and you really needed those newlines instead of a HTML-element?
May be this help you.

how to account for a newline in a mysql text field?

what's the proper syntax to account for a newline in "text" field? for example,
mysql_query("select * from table where misc_note='hello\nworld'")
isn't working
If you want mysql way then:
char(10) or char(13),char(10) depending if u want \n or \r\n
mysql_query("select * from table where misc_note=concat('hello',char(13),char(10),'world')")
EDIT: however it seems you may need this instead:
mysql_query("select * from table where misc_note like 'searchstring%'")
% indicates any number of any character that can occur, means you search for all notes startig with 'searhstring'.
You are probably outputting it to text field and not in textarea. Only textarea can display new lines.
If \n is still not working you can try \r\n.
There is a PHP Constant, PHP_EOL, which holds the End Of Line character specific to the system.
More information on this can be found at: When do I use the PHP constant "PHP_EOL"?
Not really sure if this would help you in your certain situation, but yea, just throwing it out there.

Categories