tech.inno Posted July 7, 2011 Share Posted July 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241363-mysql-5075-delete-data-in-database-php/ Share on other sites More sharing options...
tech.inno Posted July 8, 2011 Author Share Posted July 8, 2011 Can anyone help? Need more detail...? Quote Link to comment https://forums.phpfreaks.com/topic/241363-mysql-5075-delete-data-in-database-php/#findComment-1239885 Share on other sites More sharing options...
fenway Posted July 8, 2011 Share Posted July 8, 2011 You'd have to pass these values via the URL. Quote Link to comment https://forums.phpfreaks.com/topic/241363-mysql-5075-delete-data-in-database-php/#findComment-1240069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.