Page 1 of 1

Deleting rows with PHP

Posted: Thu Mar 27, 2008 10:36 am
by pritam79
I have a PHP script that displays data from mysql in a nice tabular format. I have included a submit button along with each row. Now what i want is that on clicking a button corresponding to a row should delete the entire row from the page as well from the MySql database. This is my code so far....Please help.

Code: Select all

 
<?php
include "header.php";
?>
<div id="content">
    <h2>NOTES</h2>
    <?php
$query ="SELECT date, note FROM notes";
$result = mysql_query($query);
$count=mysql_num_rows($result);
if ($count == 0) {
            echo "<br><br><strong>No notes found in database</strong>";
        }
else {      
    echo '<table style="width: 400px" align="center" border="1px">';
    echo '<form name="form1" method="post" action="">';
        echo '<tr>';
        echo '<td style="width: 100px"><b>Date</b></td>';
        echo '<td style="width: 300px"><b>Note</b></td>';
    echo '</tr>';
    while (list($date, $note) = mysql_fetch_row($result)):
    echo '<tr>';
        echo '<td style="width: 100px">'.$date.'</td>';
        echo '<td style="width: 300px">'.$note.'</td>';
        echo '<td style="border: 0px">';
        echo '<input name="delete" type="submit" id="delete" value="Delete" onclick="delrow()"></td>';      
    echo '</tr>';
    endwhile;
    echo '</form>';
    echo '</table>';
   }    
?>
 
</div>
<?php
include "footer.php";
?>
 

Re: Deleting rows with PHP

Posted: Thu Mar 27, 2008 10:51 am
by hawleyjr
Do you want the page to refresh or just remove the row from the table and the database? If so, you will want to look into using AJAX.

Re: Deleting rows with PHP

Posted: Thu Mar 27, 2008 9:41 pm
by califdon
Instead of using a "submit" button, just use either a button or a hyperlink that calls a "delete" script with the key field of the record you want to delete. It could look like this (where I'm assuming that you have a primary key column named "ID"):

Code: Select all

 echo "<a href='mydelete.php?rec=$ID'>DELETE</a>";
As hawleyjr pointed out, this isn't a very good user interface, but it should illustrate the way the fundamental process works. In a practical situation, you would probably want to use Ajax to confirm the process to the user and refresh the screen with a new query result.

Re: Deleting rows with PHP

Posted: Fri Mar 28, 2008 12:22 am
by pritam79
thanks hawleyjr & califdon...well i'l definitely try this out and get back soon again...actually i am a student and this work is for my final term project work. and i will be coming back with more queries again...

thank you
pritam79

Re: Deleting rows with PHP

Posted: Fri Mar 28, 2008 10:07 am
by kryles
Just make sure in the delete.php file that the person clicking has the rights to delete that record.

Otherwise I can randomly go and put it

mydelete.php?rec=700 and there goes record 700
mydelete.php?rec=1 and there goes record 1

etc etc

Re: Deleting rows with PHP

Posted: Fri Mar 28, 2008 10:10 am
by hawleyjr
kryles wrote:Just make sure in the delete.php file that the person clicking has the rights to delete that record.

Otherwise I can randomly go and put it

mydelete.php?rec=700 and there goes record 700
mydelete.php?rec=1 and there goes record 1

etc etc

And if you don't Google will: http://thedailywtf.com/Articles/The_Spider_of_Doom.aspx

Image

Re: Deleting rows with PHP

Posted: Fri Mar 28, 2008 10:13 am
by kryles
security could be beaten by disabiling cookies and javascript -- but management didn't quite see what was wrong with that. Instead, they told the client to NEVER copy paste content from other pages
haha management hard at work again!

Re: Deleting rows with PHP

Posted: Sat Mar 29, 2008 11:27 pm
by pritam79
How can i make my application a standalone one which wont have any dependencies?.

Re: Deleting rows with PHP

Posted: Sat Mar 29, 2008 11:30 pm
by John Cartwright
pritam79 wrote:How can i make my application a standalone one which wont have any dependencies?.
califdon wrote:Instead of using a "submit" button, just use either a button or a hyperlink that calls a "delete" script with the key field of the record you want to delete. It could look like this (where I'm assuming that you have a primary key column named "ID"):

Code: Select all

 echo "<a href='mydelete.php?rec=$ID'>DELETE</a>";
As hawleyjr pointed out, this isn't a very good user interface, but it should illustrate the way the fundamental process works. In a practical situation, you would probably want to use Ajax to confirm the process to the user and refresh the screen with a new query result.
Otherwise, create a form for every row, and insert a hidden element containing the id of the row you want to delete. Otherwise, both techniques are identical.

Re: Deleting rows with PHP

Posted: Sun Mar 30, 2008 1:05 am
by Chris Corbyn
califdon wrote:

Code: Select all

 echo "<a href='mydelete.php?rec=$ID'>DELETE</a>";
I'd just like to say that although this code was only intended to suggest an approach to solving the problem, it's a very bad idea to use hyperlinks for anything destructive. You should always use POST requests to do something destructive since many web related tools will try to browse those links (e.g. the google bot or certain browser plugins you may have installed) and wreak havoc in your system.