-2

I am working on a comment system & I created a page that admins can be able to delete comments. I have coded everything & it seems to be right but I don't know why it's not working at all...

Here's the code to the admins page:

    <html>
    <head>
        <title>Admins Page</title>
    </head>
    <body>
    <?php 
    function getCM(){
        global $con;
        $get_comment = "select * from product_comments where type='0'";
        $run_comment = mysqli_query($con, $get_comment);
        while($row_comment = mysqli_fetch_array($run_comment)){
            $cmid = $row_comment["id"];
            $cmcode = $row_comment["productcode"];
            $cmemail = $row_comment["email"];
            $cmname= $row_comment["name"];
            $cmcomment = $row_comment["comment"];
            $cmdate = $row_comment["modified_date"];
            $cmtime = $row_comment["modified_time"];
            $cmtype = $row_comment["type"];

            echo "
                <div class='container'>
                  <div id='table' class='table-editable'>
                    <span class='table-add glyphicon glyphicon-plus'></span>
                    <table class='table'>
                      <tr>
                        <th>Comment ID #$cmid</th>
                      </tr>
                      <tr>
                        <td contenteditable='true'>$cmcomment</td>
                        <td>
                          <span class='table-remove glyphicon glyphicon-remove'></span>
                        </td>
                        <td>
                          <a href='delete.php?id=$cmid'>Delete</a>
                        </td>
                      </tr>

                </div>
            ";
        }
    }
    ?>
    </body>
</html>

And here's the code to delete.php page:

 <?php 
session_start();
if (!isset($_SESSION["manager"])) {
    header("location: admin_login.php"); 
    exit();
}
require '../php_includes/init/db_conx.php'; 
require '../functions/func.php'; 

    if (isset($_GET['cmid'])){
        $comment_id = $_GET['cmid'];
        mysqli_query("DELETE FROM product_comments WHERE id='$comment_id'") or die(mysql_error());
        echo "<script>alert('Comment has been deleted!')</script>";
        header("Location: product_comments.php");
    }
?>

Please if you know what's my problem please let me know that...

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

4

There are a few things wrong here.

You didn't connect to your query mysqli_query("DELETE...

That function requires a database connection parameter be passed.

Then mysql_error() that mysql_ function does not mix with anything other than its own API, use mysqli_error($con), assuming a successful connection with mysqli_ and $con as its variable.

Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.

On the PHP side:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

should there be errors elsewhere in your code.

which there is, in this part of your code:

    echo "<script>alert('Comment has been deleted!')</script>";
    header("Location: product_comments.php");

You are outputting before header, and need to remove the echo and adding exit; for the header.

Consult: How to fix "Headers already sent" error in PHP

Then this:

<a href='delete.php?id=$cmid'>Delete</a>

You are using ?id and referencing the $_GET['cmid'] array.

That bit ^ about the "id" is called "Teach a person HOW to fish".


Footnotes:

  • I have no idea where and how you are calling the getCM() function.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

the error you have is

$comment_id = $_GET['cmid'];

change it to this

  $comment_id = $_GET['id'];

toexplain whz

<a href='delete.php?id=$cmid'>Delete</a>

U called the cmid not id. hence u need to get id

Noob
  • 154
  • 1
  • 1
  • 14