在网站EasyNote中,我遇到了换行符的问题。
在身体加载时,我设置了一个计时器,用于每 3 秒自动上传一次笔记,如下所示:
<body onload="setInterval(uploadNote,3000);current = 1;">
上传注释的代码是:
function uploadNote() {
var note = current+document.getElementById(''note'').value; //current is the number of the note selected'' because echoed
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){}
}
xmlhttp.open("GET","uploadnote.php?q="+note,true);
xmlhttp.send();
}
然后是这个php代码:
$note = $_GET["q"]; //contains both notenumber as first digit and note
echo($note."'n"); //for debugging reasons
$notenumber = substr($note, 0, 1);
$notecontent = substr($note, 1, strlen($note));
$notecontent = str_replace("'","''",$notecontent);
$notecontent = nl2br($notecontent);
echo($notecontent); //for debugging reasons
$request = 'UPDATE notes SET note'.$notenumber.' = "'.$notecontent.'" WHERE mail LIKE "'.$email.'"';
$result = mysql_query($request);
现在,问题是,文本区域中的换行符被完全擦除,因此 php-snippet 的结果是没有换行符的文本的两倍,并且在数据库中也是如此。但是,当我直接在数据库中插入它们时,在文本区域中显示换行符没有问题。
帮助将不胜感激。
编辑:更新了上传说明()函数:
function uploadNote() {
var note = current+document.getElementById(''note'').value;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){}
}
xmlhttp.open("POST","uploadnote.php",true);
xmlhttp.send("note="+note);
}
和 php:
$note = $_POST["note"];
echo($note."'n");
$notenumber = substr($note, 0, 1);
$notecontent = substr($note, 1, strlen($note));
$notecontent = mysql_real_escape_string($notecontent);
echo($notecontent);
$request = 'UPDATE notes SET note'.$notenumber.' = "'.$notecontent.'" WHERE mail LIKE "'.$email.'"';
$result = mysql_query($request);
现在的问题是没有任何效果。该注释不会在 MySQL 数据库中更新。
代码的问题:
- 不要对更改服务器上的内容使用 GET 请求,请使用 POST。
- 数据库查询需要对变量部分进行转义。对写入 SQL 的值使用
mysql_real_escape_string()
。 - 将数据保存到数据库时,不要使用任何以 html 为中心的格式。您可以在将代码输出回浏览器时使用它。
- 在文本区域中,不允许使用任何 HTML 标记,因此将
<br>
用于换行符是错误的。