2012-11-14 22 views
1

我對PHP相當陌生,最近遇到了問題。評論框問題;數據未在網站上呈現

我在我的網站上使用PHP和MySQL創建了一個評論框,它工作正常,但有一個主要問題。該評論僅在網站上呈現/存儲在數據庫中,當它適合html評論框的一行時。每當我輸入更長的文本,它根本不會保存任何東西。

這裏是我使用的代碼:

<?php 
mysql_connect("localhost","x","x"); 
mysql_select_db("fendersh_cdreviews"); 
$name=$_POST['name']; 
$comment=$_POST['comment']; 
$submit=$_POST['submit']; 
$dbLink = mysql_connect("localhost", "x", "x"); 
    mysql_query("SET character_set_client=utf8", $dbLink); 
    mysql_query("SET character_set_connection=utf8", $dbLink); 
if($submit) 
{ 
if($name&&$comment) 
{ 
$insert=mysql_query("INSERT INTO cdreviews (name,comment) VALUES ('$name','$comment') "); 
} 
else 
{ 
echo "please fill out all fields"; 
} 
} 
?> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<form action="cds.php" method="POST"> 
<table style="margin-left:15px; background-color:none; border-top: 1px dotted black; border-bottom: 1px dotted black; padding-bottom:5px;"> 
<tr><td><p style="margin-bottom:-20px;">Name:</p> <br><input type="text" name="name"/></tr> 
<tr><td colspan="2"><p style="margin-bottom:-5px;">Comment:</p> </td></tr> 
<tr><td colspan="5"><textarea name="comment" rows="3" cols="50"></textarea> 
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr> 
</table> 
</form> 
<?php 
$dbLink = mysql_connect("localhost", "x", "x"); 
    mysql_query("SET character_set_results=utf8", $dbLink); 
    mb_language('uni'); 
    mb_internal_encoding('UTF-8'); 
$getquery=mysql_query("SELECT * FROM cdreviews ORDER BY id DESC"); 
while($rows=mysql_fetch_assoc($getquery)) 
{ 
$id=$rows['id']; 
$name=$rows['name']; 
$comment=$rows['comment']; 
echo "<p><b>$name</b>; $comment </p>" 
;} 
?> 

我有出於安全原因的X替換user_name和密碼。

這就是我使用的代碼,它工作正常。

它連接到數據庫好吧;我已將我的列參數設置爲TEXT。

有什麼我應該改變嗎?

希望有人可以幫助我進一步與此。

問候

傑克

+5

您正在使用[**的**過時的數據庫API(http://stackoverflow.com/ q/12859942/19068),並應使用[現代替代](http://php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin

+0

除了這個巨大的SQL安全問題,您的應用程序也容易受到XSS攻擊。 – PeeHaa

回答

-2

您連接喜歡的三倍。並且只選擇一次DB,以下連接發生時遺忘的選擇。
嘗試使用只有一個mysqli_connect()只有一個mysqli_select_db()在代碼的開頭。是的,使用mysqli而不是mysql。 mysql已被棄用(或即將到來,我認爲)。

請把爲例此...例如,未經測試,但應該工作:

<?php 
mysql_connect("localhost","x","x"); 
mysql_select_db("fendersh_cdreviews"); 

mb_language('uni'); 
mb_internal_encoding('UTF-8'); 

if (count($_POST)) 
{ 
    $name =& $_POST['name']; 
    $comment =& $_POST['comment']; 
    $submit =& $_POST['submit']; 

    if ($submit) 
    { 
     if (strlen($name) && strlen($comment)) 
     { 
      $name = mysql_real_escape_string($name); 
      $comment = mysql_real_escape_string($comment); 
      $insert = mysql_query("INSERT INTO cdreviews (name,comment) VALUES ('$name','$comment') "); 
     } 
      else 
     { 
      echo "please fill out all fields"; 
     } 
    } 
} 
?> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<form action="cds.php" method="POST"> 
<table style="margin-left:15px; background-color:none; border-top: 1px dotted black; border-bottom: 1px dotted black; padding-bottom:5px;"> 
<tr><td><p style="margin-bottom:-20px;">Name:</p> <br><input type="text" name="name"/></tr> 
<tr><td colspan="2"><p style="margin-bottom:-5px;">Comment:</p> </td></tr> 
<tr><td colspan="5"><textarea name="comment" rows="3" cols="50"></textarea> 
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr> 
</table> 
</form> 
<?php 
    $getquery = mysql_query("SELECT * FROM cdreviews ORDER BY id DESC"); 
    while($rows = mysql_fetch_assoc($getquery)) 
    { 
     $id = $rows['id']; 
     $name = $rows['name']; 
     $comment = $rows['comment']; 
     echo "<p><b>$name</b>; $comment </p>"; 
    } 
?> 
+0

只需使用'mysqli_'而不是'mysql_'並不能解決任何問題。 [使用預準備語句和綁定參數](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection)。 – PeeHaa

+0

@PeeHaa但這裏的問題不在於使用預處理語句。這只是一個建議。 – Mario

+0

@PeeHaa問題是關於一個不起作用的代碼。由於多次調用'mysql_connect()',它不起作用。與準備好的陳述無關。你會注意到,如果你閱讀代碼。 mysql API僅被維護並且已被宣告棄用。這個理由已經足夠用於切換到mysqli了。嘿,它主要是添加一個「我」,不是嗎? – Mario