doumaoao0182 2011-08-03 15:31
浏览 116

删除论坛帖子

I have a table with the following fields: wall_posts, group_id, id, and 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.

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. 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?

  • 写回答

2条回答 默认 最新

  • douzhong2954 2011-08-03 15:50
    关注

    Your SQL query above doesn't make sense - the WHERE statement should be in the form 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, e.g. <a href="delete.php?delete=3">Delete This Post</a> for post with id 3. Then you'd do a query like this:

    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.

    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. That will help your code clean and secure.

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗