Jump to content

Recommended Posts

On my website, I recently added a comment system for games. What I want to do is add an 'X' button so that users can delete their comments if they choose. Before I can do this, I need a PHP function that will remove that specific comment from the database. Here are some details:

 

Version: MySQL 5.0.75

Structure:

CREATE TABLE `comments` (
  `commentid` int(11) NOT NULL auto_increment,
  `tutorialid` int(11) NOT NULL default '0',
  `name` text NOT NULL,
  `user_id` text NOT NULL,
  `url` text NOT NULL,
  `comment` text NOT NULL,
  `email` text NOT NULL,
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`commentid`),
  KEY `tutorialid` (`tutorialid`)
)

 

 

I'm just not sure how to go about this. I've come up with this through research, but I know it's not right:

function delete_comment()
{
  // Connect to MySQL

  $comment_id = some code to get the comment id;
  $tutorialid = some code to get the tut id;
  $user_id = $user->data['user_id'];
  $mysql_table = 'comments';
  
  $sql_delete_by_id = mysql_query("DELETE FROM `$mysql_table` WHERE `commentid` = '".$comment_id."' AND `tutorialid` = '".$tutorialid."' AND `user_id` = '".$user_id."'  "); 
  
if($sql_delete_by_id){
echo "<h1>Comment Deleted Successfully</h1>";
echo "You will now be redirected back to the last page you visited.";
echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
} else {
echo "<h1>Sorry, there was an error when attempting to delete your comment.</h1>";
echo "You will now be redirected back to the last page you visited.";
echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
}
}

 

I need the following to perfect this function.

-How could I get the tutorialid from the database when the 'X' is clicked?

-How could I get the commentid?

-I also need help with all the errors I have so far.

-Any help on how to implement this would be great as well...

 

I have not tested this yet, I know it's not right. I don't want to risk messing stuff up until I perfect it.

 

Here is the tutorial I used to make this system, it's got tons more detail if you need it!

http://www.zimmertech.com/tutorials/php/25/comment-form-script-tutorial.php

Link to comment
https://forums.phpfreaks.com/topic/241363-mysql-5075-delete-data-in-database-php/
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.