How to create a working delete button?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Xeen
Forum Newbie
Posts: 1
Joined: Mon Jul 19, 2010 5:41 am

How to create a working delete button?

Post by Xeen »

Hello, newby question here. I have a code showing comments, but I can't figure out a way to make a delete button for them, here's my code:

Code: Select all

         $result = mysql_query("SELECT * FROM comment ORDER BY P_Id DESC;");
                            while($row = mysql_fetch_array($result)) {
                            $delete = mysql_query("delete from comment where P_Id = $row[P_Id]");
                            echo "<a href='?delete&id=$row[P_Id]'>[x]</a> <span class='name'>" . $row['name'] . "</span>: " . $row['comment'] . "(" . $row['P_Id'] . ")" . "<br/><br/>"; }
so the thing is - all my comments are being deleted on page refresh and I can't have more than one comment at a time, but with this source I can delete that one. So can you give me some hints or maybe the exact code even, if it's not too much to ask? Thanks.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: How to create a working delete button?

Post by JakeJ »

You need single quotes around your variable:

Code: Select all

mysql_query("delete from comment where P_Id = '$row[P_Id]'");
To troubleshoot queries, you can also do this:

Code: Select all

mysql_query("delete from comment where P_Id = $row[P_Id]") or die(mysql_error());
Then if the query fails, it will give you some idea of why.
Post Reply