Deleting rows with PHP

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
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Deleting rows with PHP

Post 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";
?>
 
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: Deleting rows with PHP

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Deleting rows with PHP

Post 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.
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: Deleting rows with PHP

Post 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
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Deleting rows with PHP

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: Deleting rows with PHP

Post 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
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: Deleting rows with PHP

Post 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!
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: Deleting rows with PHP

Post by pritam79 »

How can i make my application a standalone one which wont have any dependencies?.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Deleting rows with PHP

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Deleting rows with PHP

Post 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.
Post Reply