Jump to content

Delete comment in comment box


jon4433

Recommended Posts

Hi,

 

I have a comment box for my current project, but before I move onto the next part, I want to have a 'Delete' link to delete the comment. I've tried it myself, but haven't had any success. Does anybody know on how to do it?

 

<?php

session_start();

if($_SESSION['username']){

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

	$connect = mysql_connect("****","****","****") or die("Could not connect to database.");
	mysql_select_db("a2148621_login") or die ("Could not find database!");

	$name=$_POST['name'];
	$comment=$_POST['comment'];

	$query = "INSERT INTO comments (name, comment) VALUES ('$name', '$comment')";
	mysql_query($query);
}
}
else
header("location: suggestion.html");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
a {text-decoration:none}
body {
background-image: url(images/sky.png);
background-size:100% 100%;
background-attachment: fixed;
background-repeat: repeat-x;
background-position: left top;
background-color: #999999; 
}
body,td,th {
color: #000;
font-family: "MS Serif", "New York", serif;
}
</style>
</head>
<body>
<div id="wrap">
<!--Header-->
    <div id="header_member" align="center"> <a href="membersarea.php"><img src="images/member_header.png" width="700" height="100" /></a></div>
    
    	<!--Log out and time-->
        <div id="info">
		<div id="date"><script type="text/javascript"> 
		var currentDate = new Date()
	  	var day = currentDate.getDate()
	  	var month = currentDate.getMonth() + 1
	  	var year = currentDate.getFullYear()
	  	document.write("<b>" + day + "/" + month + "/" + year + "</b>")
	  	var currentTime = new Date()
		var hours = currentTime.getHours()
		var minutes = currentTime.getMinutes()</script>
	     </div> 
             
            
             
        		<div id="time"><script type="text/javascript"> 
			var suffix = "AM";
	  if (hours >= 12) {
	  suffix = "PM";
	  hours = hours - 12;
	  }
	  if (hours == 0) {
	  hours = 12;
	  }

	  if (minutes < 10)
	  minutes = "0" + minutes

	  document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")</script>
          		</div>
          </div>
          
         <div id="logout"><center><?php echo "<a href='logout.php'>Log out.</a>";?></center></div> 
         
  <!--Main section which will contain everything else-->	
			 <div id="member_main_chat">
             <!--Display user information from the database-->
       		   <div id="member_right"><center>
       		     <form action="chat.php" method="Post">
                 <br />
                 	Name: <input type="text" name="name" /><br /><br />
                    <textarea name="comment" rows=4 cols=50></textarea><br /><br />
                    <input type="submit" name="submit" value="Post Comment">
			</form></center>
                
                	<div id="chat_area">
                <?php

				$connect = mysql_connect("****","****","****") or die("Could not connect to database.");
				mysql_select_db("a2148621_login") or die ("Could not find database!");

                	$query2 = "SELECT name, comment FROM comments";
                	$result=mysql_query($query2);

                	while($row=mysql_fetch_array($result)){
                	//list the comments
                		echo $row['name'] . "<br>" . $row['comment']."<br /><br><hr>";
                	}
                ?>
                	</div>
       		   </div>
                
                <!--Welcome message-->
		   <div id="member_top" align="center"><?php echo "Welcome, ".$_SESSION['username'];?></div>
                    
           	   <div id="member_left" align="center"><img src="images/navigation.png" width="105" height="30" /><a href="membersarea.php"><img src="images/home_member.png" width="105" height="30" /></a><a href="account.php"><img src="images/account.png" width="105" height="30" /></a><a href="chat.php"><img src="images/chat.png" width="105" height="30"/></a><a href="memberlist.php"><img src="images/members.png" width="105" height="30" /></a><img src="images/support.png" /></div>
         	 </div> 
             
             			<!--About us-->
                        <div id="about" align="center"><a href="membersarea.php">Home</a> | <a href="">About Us</a> | <a href="">Contact Us</a> | <a href="">FeedBack</a></div>
             
<!--Footer-->          
<div align="center" id="footer_member" style="color: #FFF">Dawncraftmc© 2011 - 2012</div>   
        
</div>

</body>
</html>

Link to comment
Share on other sites

If you're talking about in the form, most people would just select the text out of the box and delete it, or you can use the reset. If you're talking about deleting an existing comment, you would delete it from the database.

 

Either way, I don't see anything having to do with delete in your code. When you say you tried it, what did you try.

Link to comment
Share on other sites

@Muddy_Funster

I've never thought about a reset button...That could be an option!

 

@jesirose

That's what I tried to say. Deleteing an existing comment. Sorry.

I tried having a 'Delete' link beneath the comment. A existing or newly posted comment. Which would go to my delete.php, which I thought would delete the comment from both the screen and database.

 

@MSUK1

I think I know what you mean. I'll have a go at that later on.

 

Thank you for the help :)

Link to comment
Share on other sites

This is what I have in my delete.php.

 

<?php

$connect = mysql_connect("****","****","****") or die("Could not connect to database.");
mysql_select_db("****") or die ("Could not find database!");

$deleteid = $_GET['id'];

mysql_query("DELETE FROM comments WHERE id='$deleteid'");
header("location: chat.php");
?>

 

And this is my chatbox code with the 'Delete' option. (Just changed a few things from the last time that I posted it.)

<?php

$connect = mysql_connect("****","****","****") or die("Could not connect to database.");
mysql_select_db("****") or die ("Could not find database!");

$query2 = "SELECT * FROM comments ORDER BY id DESC";
$id=$rows['id'];
$name=$_POST['name'];
$comment=$_POST['comment'];
$dellink="<a href=\"delete.php?id=" . $id . "\"> Delete </a>"; 
$result=mysql_query($query2);

while($row=mysql_fetch_array($result)){
//list the comments
echo $row['name'] . "<br>" . $row['comment']."<br /> . $dellink .<br><hr>";
}
?>

Link to comment
Share on other sites

  • 3 years later...

In your chatbox code, you set $id with something uninitialized / non-existent, so that will be null.

 

$id=$rows['id'];

Then you build the delete link, again with a null value.

 

$dellink="<a href=\"delete.php?id=" . $id . "\"> Delete </a>";

 

Then you add the same link to all the items:

 

while($row=mysql_fetch_array($result)){
//list the comments
echo $row['name'] . "<br>" . $row['comment']."<br /> . $dellink .<br><hr>";
}

 

In reality the link should be constructed in the loop where you can actually access the id! So something like this:

 

while($row=mysql_fetch_array($result)){
echo $row['name'] . "<br>" . $row['comment']."<br /><a href=\"delete.php?id=" . $row['id'] . "\"> Delete </a><br><hr>";
}
Link to comment
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.