使用mysql_real_escape_string后,在textarea中显示多行

时间:2012-11-30 13:00:22

标签: php html mysql mysql-real-escape-string

我试图在将数据插入数据库之前使用mysql_real_escape_string来防止sql注入:

$data=mysql_real_escape_string($_POST['data']);

现在,数据存储如下:

That\\\'s an apostrophe.\r\n\r\nThis new line isn\\\'t displaying properly!

所以,我试图让它在从texql中拉出来之后在textarea中正确显示:

$data = nl2br($data);

无论出于何种原因,这都没有。我甚至尝试str_replace\r\n'替换<br> s,但<br>只显示在textarea中。

如何获取我的mysql中显示的内容:

That's an apostrophe.

This new line isn't displaying properly!

3 个答案:

答案 0 :(得分:2)

最佳解决方案..

NotifyCurrentCellDirty(true/false)(如果需要,可以将其插入数据库中)

public partial class frmQuantityAdjustment : Form { // In my forms partial class I created a new public BindingSource public BindingSource bsItemAdjust = new BindingSource(); } private void frmQuantityAdjustment_Load(object sender, EventArgs e) { // In my form_Load event I bound the BindingSource to my DataTable // and then the DataGridView to the BindingSource bsItemAdjust.DataSource = _dtItemAdjustList; dgvItems.DataSource = bsItemAdjust; dgvItems.Refresh(); } private void dgvItems_CellValueChanged(object sender, DataGridViewCellEventArgs e) { // Finally at the end of my CellValueChanged event I added the // following code bsItemAdjust.EndEdit(); dgvItems.NotifyCurrentCellDirty(true); dgvItems.EndEdit(); dgvItems.NotifyCurrentCellDirty(false); } (输出完全与您的输入相同)

答案 1 :(得分:1)

实际上使用mysql_real_escape_string并不能完全保护您免受SQL Injection攻击。

最好的方法是使用PDOMySQLi

答案 2 :(得分:0)

你可能打开了magic_quotes, 用它检查 echo get_magic_quotes_gpc() 否则你会加倍报价

“设置GPC(Get / Post / Cookie)操作的magic_quotes状态。当magic_quotes打开时,所有'(单引号),(双引号),\(反斜杠)和NUL都会自动转义为反斜杠。 “

顺便说一句,使用magic_quotes不是一个好的意识,尝试使用其中一个类。

PDO http://br2.php.net/manual/en/book.pdo.php 或者mysqli http://br2.php.net/manual/en/book.mysqli.php