469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

how to delete record from database through html form

I am using Windows XP as my OS and apache server. I have successfully inserted new records into database using forms and now want to delete the records from the database through using radio buttons.

Any record i'll select with radio button should be deleted on submit.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. //connect to mysql
  3. //change user and password to your mySQL name and password
  4. mysql_connect("localhost","root","root");
  5.  
  6. //select which database you want to edit
  7. mysql_select_db("mydb");
  8.  
  9. //If cmd has not been initialized
  10. if(!isset($cmd))
  11. {
  12.    //display all the news
  13.    $result = mysql_query("select * from stuinfo order by FirstName");
  14.  
  15.    //run the while loop that grabs all the news scripts
  16.    while($r=mysql_fetch_array($result))
  17.    {
  18.       //grab the title and the ID of the news
  19.       //$title=$r["title"];//take out the title
  20.       $FirstName=$r["FirstName"];//take out the id
  21.  
  22.      //make the title a link
  23.       echo "<a href='delete.php?cmd=delete&FirstName=$FirstName'></a>";
  24.       echo "<br>";
  25.     }
  26. }
  27. ?>
  28.  
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3.  <TITLE>New Document</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <? php
  7.  
  8. if($_GET["cmd"]=="delete")
  9. {
  10.     $sql = "DELETE FROM stuinfo WHERE FirstName=$FirstName";
  11.     $result = mysql_query($sql);
  12.     echo "Row deleted!";
  13. }
  14. ?>
  15. </BODY>
  16. </HTML>
  17.  
Jan 23 '10 #1

✓ answered by kovik

You'd want to use the ID of the database records as the "value" attribute of your form element. Then, use the selected ID to delete the row with that ID.

Personally, I don't feel that deletion should be a one-step process. I always provide an extra "Are you sure?" form that allows the user to change their mind, and protects them from accidental clicks.

6 12313
johny10151981
1,059 1GB
Both of these page are unrelated
You would have to do
mysql_connect and mysql_close in every unrelated page. In the second page you didnt do the mysql connection. without connection you cant delete from database.

Regards,
JOHNY
Jan 23 '10 #2
dgreenhouse
250 Expert 100+
What you'd probably want is a record id included with the list of displayed rows.

Here's a simple example of one approach:
Expand|Select|Wrap|Line Numbers
  1. ?php
  2. // Delete rows test
  3. if (isset($_POST['submit'])) {
  4.   $sql = 'delete from table_x where id in (';
  5.   foreach ($_POST['id'] as $a) {
  6.     $sql .= $a . ',';
  7.   }
  8.   $sql = substr($sql,0,strripos($sql,',')) . ')';
  9.   print $sql;
  10. }
  11. ?>
  12. <html>
  13.   <head><title>Test deleting records</title></head>
  14.   <body>
  15.     <form action="" method="post">
  16.       <table border="1">
  17.         <thead><th>Delete?</th><th>Record Detail</th></thead>
  18.         <?php
  19.           for ($i=0;$i<10;$i++) {
  20.             // Note the variable name id[] is an array...
  21.             print '<tr><td><input type="checkbox" name="id[]" value="'.$i.'" /></td>';
  22.             print "<td><p>Record#$i</p></td></tr>";
  23.           }
  24.         ?>
  25.       </table>
  26.       <input type="submit" name="submit" value="Delete Record(s)!" />
  27.     </form>  
  28.   </body>
  29. </html>
  30.  
Jan 23 '10 #3
kovik
1,044 Expert 1GB
You'd want to use the ID of the database records as the "value" attribute of your form element. Then, use the selected ID to delete the row with that ID.

Personally, I don't feel that deletion should be a one-step process. I always provide an extra "Are you sure?" form that allows the user to change their mind, and protects them from accidental clicks.
Jan 23 '10 #4
dgreenhouse
250 Expert 100+
@kovik
I agree...

In addition, it would be best that the user that is initiating the deletion operation is authenticated and probably done via an https page.

Also, it might be advisable to have a 'roll back' table set up in case the user changes their mind after the deletions are made.

i.e.
The records are pushed into a 'deletion history' table before being deleted from the main table. This table should contain additional columns such as: datetime, userid, and possibly more.

To generalize this operation, it might be best that this table is setup with a BLOB field so the records can be serialized/de-serialized versus duplicating the structure of the main table.

If this is done, an additional column should be added called something like: tablename.

That way, a general function could be constructed that would allow deletion of records from any table in the system.

Of course if there are table relations and constraints are in place that would automatically delete 'child' records, things get a bit more complicated

But I'm getting way ahead of myself here....
Jan 23 '10 #5
kovik
1,044 Expert 1GB
Or all deletable items should have an "is_deleted" flag and an expiration date. That's the easier solution for the "Undo" function. It allows for periodic database purging, and an easily programmed Undo capability.

But, like you said, getting ahead of ourselves.
Jan 23 '10 #6
dgreenhouse
250 Expert 100+
@kovik
Yep... Much better... And yep... that's the normal way of doing it...
Jan 24 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by keeps21 | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.