jon4433 Posted March 30, 2012 Share Posted March 30, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/ Share on other sites More sharing options...
Muddy_Funster Posted March 30, 2012 Share Posted March 30, 2012 add a reset button to your form? http://www.tizag.com/htmlT/htmlreset.php Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332740 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332742 Share on other sites More sharing options...
MSUK1 Posted March 30, 2012 Share Posted March 30, 2012 perhaps you might want to include a unique id to each comment, then you can use an sql query to remove the comment based on its ID. Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332771 Share on other sites More sharing options...
jon4433 Posted March 30, 2012 Author Share Posted March 30, 2012 @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 Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332782 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 You'll need to post the code for delete.php then. And yes you will need a unique ID. Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332786 Share on other sites More sharing options...
jon4433 Posted March 30, 2012 Author Share Posted March 30, 2012 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332814 Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 So is this the code that did not work? What happens when you attempt it? Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332860 Share on other sites More sharing options...
Monkuar Posted March 31, 2012 Share Posted March 31, 2012 please use mysql_real_escape_String nn hackers Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332877 Share on other sites More sharing options...
jon4433 Posted March 31, 2012 Author Share Posted March 31, 2012 Under the newly posted comment, it shows '. Delete .' and when I click on it, it just refreshes the page. I haven't learned about the mysql_real_escape_String yet monkuar. Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1332974 Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 21, 2015 Share Posted April 21, 2015 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1509565 Share on other sites More sharing options...
cyberRobot Posted April 21, 2015 Share Posted April 21, 2015 @IThinkMyBrainHurts - Note that the original post was made in March 2012. It just bubbled back to the top because of a spam post which has since been removed. Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1509568 Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 21, 2015 Share Posted April 21, 2015 lol, cheers, just advert answering anyway Quote Link to comment https://forums.phpfreaks.com/topic/260014-delete-comment-in-comment-box/#findComment-1509569 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.