472,353 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 12599
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

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

Similar topics

3
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox....
3
by: Phi | last post by:
Hi, I hope somebody could help me with this problem. I would like to make a form to add and delete records from my ms access database. I've...
6
by: JHNielson | last post by:
This is a very simple question.... I have a form that looks up Records for an unbound drop-down list. It has worked just fine up until last...
1
by: solargovind | last post by:
Hi, I have one Form in which i have one subform also which links together by one common_id(Payment_id). When i display record, I need to delete...
8
by: pukhton | last post by:
Just a quick question? I want to delete the record from my form. I have a command button in the footer area. When ever I click on the delete...
5
by: keeps21 | last post by:
A little problem I've run into is the following. I have a script that allows a user to edit a story. I have an HTML form for title and...
7
by: MyWaterloo | last post by:
HI, When a new record is opened in a main form / sub form, how do I prevent the main form from saving data if the sub form is not filled? Or how...
3
mseo
by: mseo | last post by:
hi, I need to delete the record in form and all the related record from the subform I don't know how to do so. thanks
0
by: gershwyn | last post by:
In Access 2007, I am trying to add a button to my sata entry split form that will delete the current record, after prompting, but I am running into a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.