Jump to content

[SOLVED] What did I do wrong? =P Delete comments not deleting comments.


helraizer

Recommended Posts

Hi folks,

 

http://www.helraizer.co.uk/count/comment_form.php

 

 

delete_comment.php:

<?php

error_reporting(E_ALL);

ini_set('display_errors', true);


if (isset($_GET['comment_id']) && is_numeric($_GET['comment_id'])) {

    $comment_id = $_GET['comment_id'];
    $my_ip = $_SERVER['REMOTE_ADDR'];

    mysql_connect("127.0.0.1", "user", "pass") or die("Problem connecting :" .
        mysql_error());

    mysql_select_db('db') or die("Problem selecting database :" .
        mysql_error());


    $sql1 = "SELECT * FROM `Comment`";

    $result1 = mysql_query($sql1) or die("Error perforing 1 :" . mysql_error());

    while ($row = mysql_fetch_array($result1)) {

        if ($my_ip == $row['ip'] && $comment_id == $row['id']) {

            mysql_connect("127.0.0.1", "user", "pass") or die("Problem connecting :" .
                mysql_error());

            mysql_select_db('db') or die("Problem selecting database :" .
                mysql_error());

            $sql2 = "DELETE FROM `Comment` WHERE `Comment`.`id`='$comment_id' AND `Comment`.`ip`= '$my_ip'";

            $result = mysql_query($sql2) or die("Error performing 2 :" . mysql_error());

            die("Post deletion successful <br><br> Please click <a href='./comment_form.php'>here</a> to go back. <br><br> Thank you!");

        } else {

            die("Failed: you may only delete your own posts!! <br><br> Please click <a href='./comment_form.php'>here</a> to go back and post another comment, or click <a href='./view_comments.php'>here</a> to view the comments.<br><br> Thank you!");


        }
    }

} else {
    echo "Please enter a numeric value as the comment id! No comment deletion for you otherwise. =P";
}


?> 

 

This code is intermittent... but most of the time it doesn't delete the comments it returns this message: "Failed: you may only delete your own posts!! <br><br> Please click <a href='./comment_form.php'>here</a> to go back and post another comment, or click <a href='./view_comments.php'>here</a> to view the comments.<br><br> Thank you!"

 

The thing is.. on the other page I have this code:

 

view_comments.php

if ($ip === $row['ip']) {
echo "<div class='id'><p align='right'>";
                echo '<a href="delete_comment.php?comment_id='.$id.'">delete comment</a>';
                echo "</p> </div>";
}
//rest of code

 

So that only appears if your IP address is the same as that in the database. Which to test, I use the same code in the delete_comments.php which means that the IP address from REMOTE_ADDR; must be the same as that in the database (which is sent from REMOTE_ADDR; when you submit the form). Also the id is unique (auto_increment) and is assigned to each comment. So again that has to be correct..

 

Any one know/think of a reason as to why it doesn't work? If you need more info just holler.

 

Thanks, Sam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.