Php 在不丢失格式的情况下将文本从textarea插入MySQL数据库
我正在尝试将我站点上的文本区域中的文本添加到MySQL数据库中Php 在不丢失格式的情况下将文本从textarea插入MySQL数据库,php,mysql,Php,Mysql,我正在尝试将我站点上的文本区域中的文本添加到MySQL数据库中 if (isset($_POST['text'])) { $text = sanitizeString($_POST['text']); $text = preg_replace('/\s\s+/', ' ', $text); $query = "SELECT * FROM profiles WHERE user='$user'"; if (mysql_num_rows(queryMysql($qu
if (isset($_POST['text']))
{
$text = sanitizeString($_POST['text']);
$text = preg_replace('/\s\s+/', ' ', $text);
$query = "SELECT * FROM profiles WHERE user='$user'";
if (mysql_num_rows(queryMysql($query)))
{
queryMysql("UPDATE profiles SET text='$text' where user='$user'");
}
else
{
$query = "INSERT INTO profiles VALUES('$user', '$text')";
queryMysql($query);
}
}
else
{
$query = "SELECT * FROM profiles WHERE user='$user'";
$result = queryMysql($query);
if (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
$text = stripslashes($row[1]);
}
else $text = "";
}
$text = stripslashes(preg_replace('/\s\s+/', ' ', $text));
下面是将文本添加到数据库的PHP代码
if (isset($_POST['text']))
{
$text = sanitizeString($_POST['text']);
$text = preg_replace('/\s\s+/', ' ', $text);
$query = "SELECT * FROM profiles WHERE user='$user'";
if (mysql_num_rows(queryMysql($query)))
{
queryMysql("UPDATE profiles SET text='$text' where user='$user'");
}
else
{
$query = "INSERT INTO profiles VALUES('$user', '$text')";
queryMysql($query);
}
}
else
{
$query = "SELECT * FROM profiles WHERE user='$user'";
$result = queryMysql($query);
if (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
$text = stripslashes($row[1]);
}
else $text = "";
}
$text = stripslashes(preg_replace('/\s\s+/', ' ', $text));
下面是表格的代码
<textarea name='text' cols='40' rows='3'>$text</textarea><br />
$text
但是当数据被输入时,它在数据库中正确地显示了它,但是没有正确地显示它。见下图:
输入的文本
文本在页面上的显示方式
文本在数据库中的显示方式
这是在页面上显示文本的PHP代码
$result = queryMysql("SELECT * FROM profiles WHERE user='$user'");
if (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
echo stripslashes($row[1]) . "<br clear=left /><br />
$result=queryMysql(“从配置文件中选择*,其中用户=“$user”);
if(mysql_num_行($result))
{
$row=mysql\u fetch\u row($result);
回音条斜杠($row[1])。“
希望你能帮忙
编辑:添加额外的php代码除非您使用的是富文本编辑器,如MarkDown(如Stackoverflow上的此处),否则您的文本是由CSS设置样式的,而不是由
文本区域
本身的内容设置样式的
如果问题是保留新行,那么可以在存储到数据库或从数据库检索之前,通过函数运行字符串
if (isset($_POST['text']))
{
$text = sanitizeString($_POST['text']);
$text = preg_replace('/\s\s+/', ' ', $text);
$query = "SELECT * FROM profiles WHERE user='$user'";
if (mysql_num_rows(queryMysql($query)))
{
queryMysql("UPDATE profiles SET text='$text' where user='$user'");
}
else
{
$query = "INSERT INTO profiles VALUES('$user', '$text')";
queryMysql($query);
}
}
else
{
$query = "SELECT * FROM profiles WHERE user='$user'";
$result = queryMysql($query);
if (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
$text = stripslashes($row[1]);
}
else $text = "";
}
$text = stripslashes(preg_replace('/\s\s+/', ' ', $text));
我建议检索会更好,但这只是我的意见(我不能提供任何证据来支持)。或者你可以使用
echo nl2br($test);
我希望,
sanitizeString
实际运行的是mysql\u real\u escape\u string
,否则您运行的是SQL注入的高风险。您所说的“未正确显示”到底是什么意思?我看到两个带正确空格的文本区域。如何显示该文本?添加文本的完整代码现在在上面。我应该将mysql\u real\u escape\u string
放在哪里?stripslashes
?你为什么需要它?以及sanitizeString
做什么?代码不是我写的。它是同事写的,我是Styleng为他创建网站。我想在将来添加降价。我将如何添加这个?这是一个完全不同的问题。在这里和谷歌上搜索会发现很多。但请告诉你的同事了解一些有关安全性的事情(让他了解XSS和SQL注入)。这将为您和您的客户节省很多麻烦。我最终选择了Ruby on Rails,但感谢您的回答。非常欢迎您!感谢您的接受,我很高兴您找到了达到目的的方法。