S A N T A Posted May 21, 2008 Share Posted May 21, 2008 ok so i have a blog and it allows users to post comments when they are logged in BUT if i don't like a comment that was posted or its just spam i would like to be able to delete it. here is the code i tried: if($_POST['delete']) { $db = mysql_connect($dbhost, $dbuser); mysql_select_db($dbdatabase, $db); $sql = "DELETE FROM 'comments' WHERE 'comments'.'id' = " . "" } that is where i got stuck and i had no idea how to make it specifically delete that comment and not another one. Thanks so much for helping in advance! the entire code is: <?php session_start(); require("config.php"); ?> <?php if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { $error = 1; } if($error == 1) { header("Location: " . $config_basedir); } else { $validentry = $_GET['id']; } } else { $validentry = 0; } if($_POST['delete']) { $db = mysql_connect($dbhost, $dbuser); mysql_select_db($dbdatabase, $db); #$sql = "DELETE FROM 'comments' WHERE 'comments'.'id' = " . "" } if ( $_POST['submit'] ) { if ( $_POST['comment'] == "" ) { echo "Please right a comment"; } $db = mysql_connect($dbhost, $dbuser); mysql_select_db($dbdatabase, $db); $sql = "INSERT INTO comments(blog_id, dateposted, name, comment) VALUES(" . $validentry . ", NOW(), '" . $_SESSION['USERNAME'] . "', '" . $_POST['comment'] . "');"; mysql_query($sql) or die(mysql_error(). " in $sql"); header('Location: /submited.php'); } else { //code will go here } require("header.php"); if($validentry == 0) { $sql = "SELECT entries.*, categories.cat FROM entries, categories " . "WHERE entries.cat_id = categories.id " . "ORDER BY dateposted DESC " . " LIMIT 1;"; } else { $sql = "SELECT entries.*, categories.cat FROM entries, categories " . "WHERE entries.cat_id = categories.id AND entries.id = " . $validentry . " ORDER BY dateposted DESC LIMIT 1;"; } $result = mysql_query($sql)or die(mysql_error()); $row = mysql_fetch_assoc($result); echo "<h2>" . $row['subject'] . "</h2><br />"; echo "<i>In <a href='viewcat.php?id=" . $row['cat_id'] ."'>" . $row ['cat'] ."</a> - Posted on " . date("D jS FY g.iA", strtotime($row['dateposted'])) ."</i>"; if(isset($_SESSION['ADMIN']) == TRUE) { echo" [<a href='updateentry.php?id=" . $row['id'] . "'>edit</a>]"; } echo "<p>"; echo nl2br($row['body']); echo "</p>"; $commsql = "SELECT * FROM comments WHERE blog_id = " . $validentry . " ORDER BY dateposted DESC;"; $commresult = mysql_query($commsql); $numrows_comm = mysql_num_rows($commresult); if(isset($_SESSION['USERNAME']) == TRUE) { if($numrows_comm == 0) { echo "<p>No comments.</p>"; } else { $i = 1; while($commrow = mysql_fetch_assoc($commresult)) { echo "<a name='comment" . $i . "'>"; echo "<h3>Comment by " . $commrow['name'] . " on " . date("D jS F Y g.iA", strtotime($commrow['dateposted'])) . "</h3>"; if(isset($_SESSION['ADMIN']) == TRUE) { echo"[<a name='delete' href='viewentry.php?id=" . $row['id'] . "'>delete</a>]"; } echo $commrow['comment']; $i++; } } ?> <table> <tr> <td> <h3>Leave a comment</h3> </td> </tr> <form action="<?php echo $SCRIPT_NAME . "?id=" . $validentry; ?>" method="post"> <tr> <td>Comments</td> <td><textarea name="comment" rows="10" cols="50"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Add Comment"></td> </tr> <tr> <td> <font size="-2">Do not post inapropriate comments or they will be deleted</font> </td> </tr> </form> </table> <?php } else { echo "Login to view/add comments!"; } require("footer.php"); ?> Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted May 21, 2008 Share Posted May 21, 2008 Why not just create a delete column and mark it as true or 1 on delete. then change your sql query to only show comments that have the delete set to 0. Quote Link to comment Share on other sites More sharing options...
S A N T A Posted May 21, 2008 Author Share Posted May 21, 2008 sorry i don't get it? I'm very new to PHP. Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 21, 2008 Share Posted May 21, 2008 the comment off the current memo needs to be in the link.......... echo"[<a name=delete' href='viewentry.php?id=" . $numrows['id] . "'>delete</a>]"; Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted May 21, 2008 Share Posted May 21, 2008 Instead of deleting the comments create a new column in your comments table called deleted. That column could be true / false 1 / 0... whatever you decide. Then instead of deleting comments you can update them and set the deleted column to true or 1 or whatever... That way if people delete comments that are rude or foul and people complain you can have evidence of what's going on. You may want to create a purge page that will actually delete all the comments at a later date but for now it shouldn't be too bad. You will need to change the SQL query that pulls comments from the database so that only non deleted comments are pulled. Quote Link to comment Share on other sites More sharing options...
S A N T A Posted May 21, 2008 Author Share Posted May 21, 2008 how would i do that and how would i make it delete the selected one? Quote Link to comment 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.