Hey, I am trying to code a simple guestbook, and am trying to add a delete comment feature. So far I have: viewguestbook.php <?php include('connect.php'); mysql_select_db('guestbook'); $sql=("SELECT * from guestbook ORDER by datetime DESC"); $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){?> <table border="1"> <tr> <td>Comment ID:</td><td width=200><?php echo $rows['id'];?></td> </tr> <tr> <td>Name:</td><td width=200><?php echo $rows['name']; ?></td> </tr> <tr> <td>Email</td><td width=200><?php echo $rows['email']; ?></td> </tr> <tr> <td>Comment:</td><td width=200><?php echo $rows['comment']; ?></td> </tr> <tr> <td>Date Posted:</td><td width=200><?php echo $rows['datetime']; ?></td> </tr> <tr><td><a href="deleteac.php?id=<? echo $rows['id']; ?>">Delete Comment</a></td></tr> </table> <?PHP } ?> PHP: deleteac.php <?php include('connect.php'); mysql_select_db('guestbook'); $id=$_GET['id']; $sql="DELETE FROM guestbook WHERE id='$id'"; $result=mysql_query($sql); if($result){ echo 'Deleted!'; } mysql_close(); ?> PHP: Any help would be appreciated, thanks!
Thats your problem. Using a variable like that tells PHP to use it as a string and not parse the value when the query gets executed. Change it to this: $sql = "DELETE FROM guestbook WHERE id=\"$id\""; PHP: OR if you must use single quotes: $sql = "DELETE FROM guestbook WHERE id='" . $id . "'"; PHP:
You have to verify that the page is getting the values properly. To verify the names and values by get or post method user var_dump($_REQUEST); die(); at the top of your script. If you will see that the id and its value is passing properly and page is getting the id element then try to use the following code to declare the variable. if(@$_GET['id']){ $id_new=$_GET['id']; } Then execute and place id variable in the query. $query="DELETE FROM guestbook WHERE id='".$id_new."'"; I hope it will help you a lot. Thnx
I worked out how to do it. It turns out that the delete comment link was wrong. It was: <tr><td><a href="deleteac.php?id=<? echo $rows['id']; ?>">Delete Comment</a></td></tr> PHP: This didn't work as the <? didn't open PHP tags. <tr><td><a href="deleteac.php?id=<?php echo $rows['id']; ?>">Delete Comment</a></td></tr> PHP: This Worked Thanks for all your help.