繁体   English   中英

通过settimer-function将textarea.value存储到MySQL数据库,而不会丢失换行符

[英]Store textarea.value via settimer-function to MySQL database without losing newlines

EasyNote网站 ,换行符出现问题。

在主体上载时,我设置了一个计时器,每3秒自动上载音符,如下所示:

<body onload="setInterval(uploadNote,3000);current = 1;">

并且uploadNote的代码是:

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);

现在的问题是,textarea中的换行符已被完全擦除,因此php-snippet的结果是没有换行符的文本的两倍,数据库中也是如此。 但是,当我将换行符直接插入数据库时​​,在换行符中显示换行符没有问题。

帮助将不胜感激。

编辑:更新了uploadNote()函数:

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数据库中更新。

您的代码存在的问题:

  1. 不要使用GET请求更改服务器上的内容,而要使用POST。
  2. 数据库查询需要转义可变部分。 在写入SQL的值上使用mysql_real_escape_string()
  3. 将数据保存到数据库时,请勿使用任何以html为中心的格式。 您可以在将代码输出回浏览器时使用它。
  4. 在文本区域内,不允许使用任何HTML标记,因此对换行符使用<br>是错误的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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