简体   繁体   English

删除论坛帖子

[英]Deleting forum posts

I have a table with the following fields: wall_posts, group_id, id, and user_id. 我有一个包含以下字段的表:wall_posts,group_id,id和user_id。

Wall_posts is user entries, group_id is imported from another table and is just a unique group id, id is just a simple counter, and user_id gets the user id from the session. Wall_posts是用户条目,group_id是从另一个表导入的,只是一个唯一的组ID,id只是一个简单的计数器,user_id从会话中获取用户ID。

My code gets rid of all the wall_posts if you press the delete button by comparing the user id to the user in session. 如果您通过将用户ID与会话中的用户进行比较来按下删除按钮,则我的代码将删除所有wall_posts。 I'm trying to find a way to delete individual posts and not all the posts by the user. 我正试图找到一种删除单个帖子的方法,而不是用户删除所有帖子。

Here is the code: 这是代码:

if (isset($_POST['delete'])) {
    $current_user = $_SESSION['user_id'];
    $result = mysql_query("SELECT * FROM group_posts");
    while ($user_id = mysql_fetch_array($result)) {
        $id = $user_id['user_id'];
    }
    if ($current_user == $id) {
        mysql_query("DELETE FROM group_posts WHERE $current_user = $id") or die(mysql_error());
    }
}

How can I bound the delete button to individual posts instead of deleting all the posts made by the user currently in session? 如何将删除按钮绑定到单个帖子而不是删除当前在会话中的用户所做的所有帖子?

Your SQL query above doesn't make sense - the WHERE statement should be in the form WHERE column_name = value . 上面的SQL查询没有意义 - WHERE语句应该采用WHERE column_name = value的形式。

Assuming id is the primary key for group_posts, as you're displaying posts, create a link for each post created by the author, eg <a href="delete.php?delete=3">Delete This Post</a> for post with id 3. Then you'd do a query like this: 假设id是group_posts的主键,当您显示帖子时,为作者创建的每个帖子创建一个链接,例如<a href="delete.php?delete=3">Delete This Post</a>发布id 3.然后你会做这样的查询:

DELETE FROM group_posts WHERE id = postIdValueHere

Using the code pattern you have above: 使用上面的代码模式:

if (isset($_POST['delete']) && $_POST['delete'] > 0) {
$current_user = $_SESSION['user_id'];
$post_id = (int) $_POST['delete'];

if ($current_user == $id) {
    mysql_query("DELETE FROM group_posts WHERE id = $post_id AND user_id = $id") or die(mysql_error());
}

That query ensures that only posts with a given ID, created by the current author, can be deleted. 该查询确保只能删除当前作者创建的具有给定ID的帖子。

Does that answer your question? 这是否回答你的问题?

Once you get more comfortable with SQL, you might also want to look into using prepared statements with mysqli or PDO . 一旦你对SQL更加熟悉,你可能还想研究使用mysqliPDO的预处理语句。 That will help your code clean and secure. 这将有助于您的代码清洁和安全。

I'm assuming your delete buttons are simple links. 我假设你的删除按钮是简单的链接。 Your links must contain all the information to delete a post. 您的链接必须包含删除帖子的所有信息。 One way would be to pass a post id as a GET variable (eg link="myurl.com/posts/delete?id=#"). 一种方法是将post id作为GET变量传递(例如link =“myurl.com/posts/delete?id=#”)。

Your script would then at first make sure the current user is allowed to delete the post. 然后,您的脚本将首先确保允许当前用户删除帖子。 For example: 例如:

$user_id = $_SESSION['user_id'];
$post_id = (int) $_GET['id'];
if(canDelete($user_id, $post_id))
{
    // assuming post_id is unique for every post
    $sql = sprintf("DELETE FROM group_posts WHERE id = %d", $post_id);
    mysql_query($sql);
}

Of course, you'd have to implement canDelete($user_id) yourself. 当然,你必须自己实现canDelete($user_id)

By the way, "DELETE FROM group_posts WHERE $current_user = $id" always deletes every record in your table. 顺便说一句, "DELETE FROM group_posts WHERE $current_user = $id"总是删除表中的每条记录。 At first you're comparing if $current_user equals $id and if they do happen to be equal, your query would look something like WHERE 1 = 1 . 首先,你要比较$current_user等于$id ,如果它们恰好相等,你的查询看起来就像WHERE 1 = 1 I think you mean "DELETE FROM group_posts WHERE user_id = '$id'" 我认为你的意思是"DELETE FROM group_posts WHERE user_id = '$id'"

EDIT : It seems you want to use ajax for deleting your posts. 编辑 :似乎你想使用ajax删除你的帖子。 I recommend using jQuery or any other proper javascript framework as it saves you time. 我建议使用jQuery或任何其他适当的JavaScript框架,因为它可以节省您的时间。 Here is a link from the jQuery documentation describing how to make an ajax call to the server and a similar question to help you understand better. 以下是jQuery文档中的链接,该文档描述了如何对服务器进行ajax调用以及类似的问题,以帮助您更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM