View Full Version : Remove Database Entry (PHP)

07-07-2011, 06:49 PM
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. How should I go about this?

Here is how the comment is added to the database:

$tuturl = $_POST["tuturl"];
$tutid2 = $_POST["tutid2"];
$name = $_POST["name"];
$url = $_POST["url"];
$email = $_POST["email"];
$message = $_POST["message"];
$user_id = $_POST["user_id"];

$sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now(), user_id='$user_id' ");

Any help would be greatly appreciated!

*Note - More Detail*
Here's the tutorial I followed for my comment system:

07-07-2011, 07:14 PM
I found this on another site

$sql_delete_byindex = "DELETE FROM `$mysql_table` WHERE `index` = '".$rec_index."' AND `key` = '".$key."'";

I know very little about my sql, only started working with it a couple weeks ago. Could I use this? Something like:

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."' ") or die(mysql_error());


Would this code work? I'm a bit worried about testing it, I don't want to mess anything up.

07-08-2011, 12:47 AM
I'm a bit worried about testing it, I don't want to mess anything up.

All testing/development is normally done in a testing environment not a "live" production environment.

One option is to download a free ready to go ("just add water") web server, like XAMPP (http://www.apachefriends.org/en/xampp.html), and do all your testing on that. XAMPP comes with Apache, PHP and MySQL preconfigured to work together.