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

In your code, that error means this line is false

 

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

 

So echo $my_ip, $row['ip'], $comment_id, $row['id'] to see if they are what they should be.

I found the problem.. the $SQL1 query was just SELECT * FROM Comment.. so it only picked the first row from the database.

 

So I changed it to "SELECT * FROM `Comment` WHERE `Comment`.`id`='$comment_id'";

 

Now it works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.