Jump to content

Delete MySQL data with a link?


toxic27

Recommended Posts

I recently added a comment script (not mine) to my site and I'm still a beginner at all the MySQL and PHP stuff. Now I have everything done with the 'If..Else' statement to make sure that I'm an admin when I need to delete a comment but now I need a 'delete' link that will delete the comment if needed. Any help? Just ask if you need more details.

Link to comment
Share on other sites

What code are you working with now?It should be a simple task, actually. One query to find the correct comment and delete it.Sample code here, simply change 'user' with 'comment', or what ever you have used in your DB.

		// Make the query.		$query = "DELETE FROM users WHERE user_id=$id";				$result = @mysql_query ($query); // Run the query.		if (mysql_affected_rows() == 1) { // If it ran OK.					// Print a message.			echo '<h1 id="mainhead">Delete a User</h1>		<p>The user has been deleted.</p><p><br /><br /></p>';					} else { // If the query did not run OK.			echo '<h1 id="mainhead">System Error</h1>			<p class="error">The user could not be deleted due to a system error.</p>'; // Public message.		}

Link to comment
Share on other sites

Well where should I put the code you gave? BTW, I'm using PHP for the main part of it. Here's a snippet of the code..

if($session->isAdmin()){echo '<td align="left" style="text-indent: 14pt" colspan="2"><h5> '.stripslashes($info2->comment).'<br><a href="">DELETE</a></h5><br></td>';}else{echo '<td align="left" style="text-indent: 14pt" colspan="2"><h5> '.stripslashes($info2->comment).'</h5><br></td>';}echo '</tr><tr>';

So that delete link you see, I need that to be the link to click to delete the selected comment. So where do I add your code? Thanks for the help!

Link to comment
Share on other sites

In another page, ie: delete_comment.php and the link for the Admin to delete that comment should point to it in the anchor tag.

if($session->isAdmin()){echo '<td align="left" style="text-indent: 14pt" colspan="2"><h5> '.stripslashes($info2->comment).'<br><a href="delete_comment.php?comment_id=???">DELETE</a></h5><br></td>';}else{echo '<td align="left" style="text-indent: 14pt" colspan="2"><h5> '.stripslashes($info2->comment).'</h5><br></td>';}echo '</tr><tr>';

This is a difficult thing to explain since you apparently are cutting and pasting code, but the snippet I supplied should be in its own file. I am uncertain what to use for the comment identifier, as well, so I substituted a series of question marks. It is also lacking the 'rest of the file' which you should have to complete the file. (ie: error checking) This is merely one of several files that should be part of a more complete package for the Comment system you have added. I would encourage you to Google for another system to use that includes the Admin side of a more complete Comment system.

Link to comment
Share on other sites

I tried the code you gave me but it gave me the 'else' error message. Here's all my code..The "Delete" link..

<a href="delete_comment.php?comment_id=$id">DELETE</a>

The 'delete_comment.php' page..

<?php include('database.php');?><?		// Make the query.		$query = "DELETE FROM comments WHERE id=$id";				$result = @mysql_query ($query); // Run the query.		if (mysql_affected_rows() == 1) { // If it ran OK.		   header("Location: ../index.php");		} else { // If the query did not run OK.			echo '<h1 id="mainhead">System Error</h1>			<p class="error">The user could not be deleted due to a system error.  <a href="index.php">Home</a></p>'; // Public message.		}?>

Now for the '$id' thing, is that simply just the unique ID each comment has in the PHPMyAdmin page? Thanks for all the help!

Link to comment
Share on other sites

Try this:

// Check for a valid user ID through GETif ( (isset($_GET['comment_id'])) && (is_numeric($_GET['comment_id'])) ) { 	$id = $_GET['comment_id'];	} else { // No valid ID, kill the script.	echo '<h1 id="mainhead">Page Error</h1>	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';	include ('./includes/footer.html'); 	exit();}		// Make the query.		$query = "DELETE FROM comments WHERE id=$id";	 		$result = @mysql_query ($query); // Run the query.		if (mysql_affected_rows() == 1) { // If it ran OK.					// Print a message.			echo '<h1 id="mainhead">Delete a User</h1>		<p>The user has been deleted.</p><p><br /><br /></p>';  				} else { // If the query did not run OK.			echo '<h1 id="mainhead">System Error</h1>			<p class="error">The user could not be deleted due to a system error.</p>'; // Public message.			echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.		}

Link to comment
Share on other sites

It's coming up with the error message. This is the code I'm using..

<html><head></head><body><?mysql_connect("localhost", "kingps3", "**");mysql_select_db("kingps3"); // Check for a valid user ID through GETif ( (isset($_GET['comment_id'])) && (is_numeric($_GET['comment_id'])) ) { 	$id = $_GET['comment_id'];	} else { // No valid ID, kill the script.	echo '<h1 id="mainhead">Page Error</h1>	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';	exit();}		// Make the query.		$query = "DELETE FROM comments WHERE id=$id";	 		$result = @mysql_query ($query); // Run the query.		if (mysql_affected_rows() == 1) { // If it ran OK.					// Print a message.			echo '<h1 id="mainhead">Delete a User</h1>		<p>The user has been deleted.</p><p><br /><br /></p>';  				} else { // If the query did not run OK.			echo '<h1 id="mainhead">System Error</h1>			<p class="error">The user could not be deleted due to a system error.</p>'; // Public message.			echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.		}?></body></html>

Link to comment
Share on other sites

Okay, just checking, but how is the comment list created? I assume you are getting the "The user could not be deleted due to a system error.".Is there a Comment_id for each comment? or is it asking to find comment_id= $id ?The page that list the Comments needs to add the correct 'id' value for each listed Comment. The value should match the phpmyadmin screen you posted above.*edit*You really need to remove the password and DB name up in that post.

Link to comment
Share on other sites

Oops, last time I deleted the password but this time I forgot. No worries though, I changed my pw and all for PHPMyAdmin. Thanks for letting me know!Alright, and back to the comment problem. What do you mean by an ID for each comment? When anytime anyone posts a comment, an ID is given to the comment. Is that what you meant?

Link to comment
Share on other sites

So the script that lists the comments out, and where the 'delete' link for that comment is, should identify each line by using the comment table "id", right? Is the script doing that?It would help to see a link to the demo page. Then i could determine whether the unique comment_id is there or not. Is this site "live" or on your local machine only?

Link to comment
Share on other sites

Here you go:http://www.kingps3.com/_r2.phpIf you scroll down, you will see some comments. If you're logged onto an admin account, you will see the delete link right below each comment which has this code:

<a href="delete_comment.php?comment_id=$id">DELETE</a>

If you want I can give you an admin account really quick so you can see the delete link.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...