1
我編寫了一些表單來發送文本到MySql。我的問題之一是保持線路中斷。在Google上發現了一些代碼,但我不知道如何使用它。有些嘗試不是很成功。在Textarea和MySQL上保留換行符
我發現的代碼是下面我不知道如何設置。
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;
}
我發現的另一個代碼是這個,但如果我嘗試從數據庫中讀取它,換行符將不起作用。
if (isset($_POST['submit']))
{
$text = trim($_POST['text']);
$text = stripslashes($text);
$text = htmlspecialchars($text);
echo 'you entered:<br><br>' . nl2br($text);
}
這是我的代碼,我想植入它。
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>
歡呼傢伙
是在應該是一些HTML代碼textarea的輸入?如果是這樣,則必須使用_br_標記指定HTML中的換行符。如果輸入是純文本的,則必須將其轉義。也許我不明白你的問題。 – acondolu 2012-07-07 18:15:55
不應該只是純文本 – kara 2012-07-07 18:17:20
好吧,那麼我認爲你應該HTML轉義'$ _POST ['title']',然後應用'nl2br'並最終sql轉義。 – acondolu 2012-07-07 18:23:27