简体   繁体   中英

Insert code snippet into database and display it identically in textarea

How would I be able to insert code snippet into a database and then display that code in a textarea identically to when I inserted it.

When comparing the code in & out, they are never the same, for some reason I can't get it to work.

$db = new sqlite3('test.db');
$r = $db->query("select * from test where id='1'");
$f = $r->fetchArray();

echo "<textarea rows='10' style='width:500px;'>$f[data]</textarea>";
$db->close();

this is the code I'm testing

't apple \n\r 

♦   &diams; &#9830; black (solid) diamond suit

<textarea></textarea>
$£%^&*()!@">RWH{{@£})"":?'
<form>dfddf
<input type="button">
</form>

How can I insert it into the database correctly to display the data identically within the textarea?

Highly recommend not storing code in your database, this can lead to some pretty serious security flaws, but you can use htmlentities() when adding the code to the database.

Keep in mind this is not going to make you fully secure, however, it will at least change the tags to symbols.

htmlentities — Convert all applicable characters to HTML entities

This will do what you need to be done to store the result in your database:

$string = '<h1>This is a Heading 1</h1>';

Now when running your sql query, add the htmlentities function with your string:

htmlentities($string);

If you print that you will see:

<h1>This is a Heading 1</h1>

Instead of:

This is a Heading 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM