2013-04-11 86 views
0

對不起,如果這是一個重複的問題,或者是一個費力的問題,但我不知道這是什麼名字,它對我來說只會有幫助。我知道如何正確執行此操作。使用*刪除評論*功能

的代碼是:

if(isset($_POST['RemoveComment'])){ 

mysql_query("DELETE FROM `comments` WHERE `id`='".$commentid."' "); 

} 

這部作品的特徵之前的方式是,它回聲出來的意見表,它從一個直接的配置文件名稱選擇。我嘗試使用這個刪除評論系統的方法是將同一個表中(每條評論旁邊)的按鈕提供給配置文件所有者或管理員。它會刪除按鈕被按下的ID。但是,上面的代碼只能刪除所有評論,有些甚至可以從其他配置文件中刪除。

我不知道這樣的函數的名稱,但我很喜歡將它命名爲「通用函數」來處理任何評論,請幫助我,我將在多重特性我的網站和我缺乏這些功能的知識和名稱。

額外的代碼:(變量)

$poster = $get_comments['poster']; 
$comment = $get_comments['message']; 
$posted = $get_comments['posted']; 
$commentid = $get_comments['id']; 
+0

您正在使用[an **過時的**數據庫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 2013-04-11 19:58:52

回答

1

我認爲你正在尋找的名稱是function,這是可重複使用的...

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world'); 

/* check connection */ 
if (!$link) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

function deleteComment($link, $messageId) { 
    $stmt = mysqli_prepare($link, "DELETE FROM comments WHERE id = ?"); 
    mysqli_stmt_bind_param($stmt, 'i', $messageId); 

    /* execute prepared statement */ 
    mysqli_stmt_execute($stmt); 

    /* close statement and connection */ 
    mysqli_stmt_close($stmt); 
} 

// Call the function 
deleteComment($link, 1); 

/* close connection */ 
mysqli_close($link); 

注意mysql_功能棄用。請改爲了解prepared statements,並使用PDOMySQLi - this article將幫助您決定哪個。

+0

您是否認爲我可以使用PHP $ _GET表單方法來獲取按鈕被按下的實際域並嘗試從那裏刪除查詢? – user2230442 2013-04-11 20:02:45

+0

@ user2230442遠程或本地?像'/delete.php?messageId = 5'?技術上,是的。但這並不安全。 – Kermit 2013-04-11 20:03:35

+0

感謝您解決潛在問題的解決方案,但該功能無法正常工作,這可能是因爲我不知道如何處理$ link變量,但我已經在它之前調用了連接腳本。 – user2230442 2013-04-11 20:14:32