user2230442
user2230442

Reputation: 13

Working with a *delete comment* feature

I'm sorry if this is a duplicate, or a low-effort question, but I do not know what the name of this is, and it will be nothing but helpful to me if I knew how to execute this correctly.

The code is:

if(isset($_POST['RemoveComment'])){

mysql_query("DELETE FROM `comments` WHERE `id`='".$commentid."' ");

}

The way this works prior to the feature is that it echos out a table of comments which it selects from a direct profile name. The way I am trying to work this delete comment system is by giving the button in the same table (next to every comment) to the profile owner or an administrator. It would delete the comment with the ID the button was pressed on. However, the code above is only capable of deleting all the comments, and some even from other profiles.

I do not know the name of a function like this, but I do enjoy naming it as a "Universal Function" to work on any comments, please help me with this, I am going to be using this on MULTIPLE features with my website and I lack the knowledge and names of these functions.

Extra code: (the variables)

$poster = $get_comments['poster'];
$comment = $get_comments['message'];
$posted = $get_comments['posted'];
$commentid = $get_comments['id'];

Upvotes: 0

Views: 145

Answers (1)

Kermit
Kermit

Reputation: 34055

I think the name you're looking for is a function, which is reusable...

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

/* check connection */
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

function deleteComment( $link, $messageId ) {
    $stmt = mysqli_prepare($link, "DELETE FROM comments WHERE id = ?");
    mysqli_stmt_bind_param($stmt, 'i', $messageId);

    /* execute prepared statement */
    mysqli_stmt_execute($stmt);

    /* close statement and connection */
    mysqli_stmt_close($stmt);
}

// Call the function
deleteComment( $link, 1 );

/* close connection */
mysqli_close($link);

Be aware that mysql_ functions are deprecated. Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Upvotes: 1

Related Questions