2011-09-14 10 views
0

我無法弄清楚爲什麼我在我的代碼中得到這個錯誤。任何人都可以幫我解決這個問題嗎?解析錯誤:語法錯誤,意外的T_STRING在43行C: ... GuestbookPHP.php

只是爲了讓你知道,我的第43行是

INSERT INTO commentList

<?php 

$dbServer='localhost'; 
$dbUser='root'; 
$dbPass='password'; 
$dbName='Guestbook'; 

mysql_connect("$dbServer", "$dbUser", "$dbPass") or die(mysql_error()); 
mysql_select_db("$dbName") or die(mysql_error()); 


mysql_query("CREATE TABLE commentLlist(
    id INT NOT NULL AUTO_INCREMENT, 
     PRIMARY KEY(id), 
     name VARCHAR(30), 
     comment VARCHAR(160))") 
    or die(mysql_error()); 


    // Retrieving data from the url 
    $name = $_GET['name']; 
    $comment = $_GET['comment']; 


    if ($name != null && $comment != null) { 
     INSERT INTO commentList 
     VALUES("$name", "$comment"); 

    } else if ($name == null) { 
     INSERT INTO commentList 
     VALUES("Anonymous", "$comment"); 

    } else if ($comment == null) { 
      INSERT INTO commentList 
      VALUES("$name", " has nothing to say!"); 



    $display_string = "<li>"; 
    $display_string .= "$name"; 
    $display_string .= " says"; 
    $display_string .= "$comment"; 
    $display_string .= "<li>"; 


    echo $display_string; 


?> 
+0

當出現錯誤時,評論和姓名字段的內容是什麼?它是一直髮生的,還是隻發生在某些評論上?這是一個品牌博客軟件,或家庭釀造? – Drazisil

+0

作爲一個方面說明,你的表叫做commentLlist,而不是commentList – Joe

回答

3

下面的代碼是錯誤的:

if ($name != null && $comment != null) { 
    INSERT INTO commentList 
    VALUES("$name", "$comment"); 

你想要做的就是發送此查詢到mysql權?

所以你需要使用mysql_query函數。所以你應該寫這樣的東西:

if ($name != null && $comment != null) { 
    mysql_query("INSERT INTO commentList VALUES('".$name."', '".$comment."')"); 
} 

也有一個你的最後的else語句後缺少大括號。


此外,將用戶輸入直接放入您的查詢是一個真正的壞主意。你應該使用mysql_real_escape_string()來逃避你的參數。

if ($name != null && $comment != null) { 
    mysql_query("INSERT INTO commentList 
     VALUES('".mysql_real_escape_string($name)."', '".mysql_real_escape_string($comment)."')"); 
} 
+0

嘿,謝謝你指出所有的錯誤!它現在工作完全正常 – BurninatorDor

+0

如果它解決了你的問題,你可以接受答案。謝謝。 – Alfwed

相關問題