2013-04-11 103 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