Php 在Textarea和MySQL上保持换行
我编写了一些表单来向MySql发送文本。我的问题之一是保持线路中断。在谷歌上找到了一些代码,但我不知道如何使用它。有些尝试并没有真正成功 我找到的代码是下面我不知道如何设置的代码Php 在Textarea和MySQL上保持换行,php,mysql,line,break,Php,Mysql,Line,Break,我编写了一些表单来向MySql发送文本。我的问题之一是保持线路中断。在谷歌上找到了一些代码,但我不知道如何使用它。有些尝试并没有真正成功 我找到的代码是下面我不知道如何设置的代码 function my_nl2br($string){ $string = str_replace("\n", "<br />", $string); if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string,
function my_nl2br($string){
$string = str_replace("\n", "<br />", $string);
if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>", $string);
}
}
}
return $string;
}
函数my_nl2br($string){
$string=str\u replace(“\n”,“
,$string”);
if(preg_match_all('/\(.*?\/'),$string,$match)){
foreach($a匹配){
foreach($a为$b){
$string=str_replace(“”.$b.”,“”.str_replace(“
,“”,$b)。“”,$string);
}
}
}
返回$string;
}
我发现的另一个代码是这样的,但如果我试图从数据库中读取它,换行符将不起作用
$title = real_escape_string(nl2br(htmlspecialchars($_POST['title'])));
if(isset($\u POST['submit']))
{
$text=trim($_POST['text']);
$text=条带斜杠($text);
$text=htmlspecialchars($text);
echo“您输入:
”.nl2br($text);
}
这是我想植入的代码
if(isset($\u POST['title']))
{
$title=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['title']);
$description=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['description']);
$applepart=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['applepart']);
$partnumber=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['partnumber']);
$productcode=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['productcode']);
$compatibility=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['compatibility']);
$url\u bild=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['url\u bild'));
$price=mysql\u real\u escape\u字符串(htmlspecialchars($\u POST['price']);
$insert=mysql\u查询(“插入到`adressbuch`(`title`、`description`、`applepart`、`partnumber`、`productcode`、`compatibility`、`url\u bild`、`price`)值(`title`、`description`、`applepart`、`partnumber`、`productcode`、`partnumber`、`compatibility`、`url\u bild`、`price`)中);
如果(!$insert)
{
die('Eintrag konnte nicht gespeichert werden:'.mysql_error());
}
}
?>
Neuer Eintrag:
标题
说明
苹果零件
零件号
产品代码
兼容性
图像
价格
干杯伙计们要在数据库中存储标题,请尝试:
文本区域中的输入应该是一些HTML代码吗?如果是这样,HTML中的换行符必须用br标记指定。如果输入是纯文本,则必须对其进行转义。也许我不明白你的问题,不。应该是纯文本好吧,那么我想你应该用HTML escape
$\u POST['title']
,然后应用nl2br
,最后是sql escape。谢谢你,但作为初学者,我的问题是如何添加它:)我成功了。。谢谢你(多鲁:))
if(isset($_POST['title']))
{
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
$applepart = mysql_real_escape_string(htmlspecialchars($_POST['applepart']));
$partnumber = mysql_real_escape_string(htmlspecialchars($_POST['partnumber']));
$productcode = mysql_real_escape_string(htmlspecialchars($_POST['productcode']));
$compatibility = mysql_real_escape_string(htmlspecialchars($_POST['compatibility']));
$url_bild = mysql_real_escape_string(htmlspecialchars($_POST['url_bild']));
$price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
$insert = mysql_query("INSERT INTO `adressbuch` (`title`,`description`,`applepart`,`partnumber`,`productcode`,`compatibility`,`url_bild`,`price`) VALUES ('$title','$description','$applepart','$partnumber','$productcode','$compatibility','$url_bild','$price')");
if (!$insert)
{
die('Eintrag konnte nicht gespeichert werden: ' . mysql_error());
}
}
?>
<form method="POST" action="?page= ">
<span>Neuer Eintrag:</span> <br />
<span>Title</span><input type="text" name="title" /> <br />
<span>Description</span><textarea cols="16" rows="5" name="description"></textarea> <br />
<span>Apple Part</span><input type="text" name="applepart" /> <br />
<span>Part Number</span><input type="text" name="partnumber" /> <br />
<span>Product Code</span><input type="text" name="productcode" /> <br />
<span>Compatibility</span><input type="text" name="compatibility" /> <br />
<span>Image</span><input type="text" name="url_bild" /> <br />
<span>Price</span><input type="text" name="price" /> <br />
<input type="submit" value="Speichern"/> <br />
</form>
$title = real_escape_string(nl2br(htmlspecialchars($_POST['title'])));