473,624 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to delete record from database through html form

15 New Member
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
6 12803
johny10151981
1,059 Top Contributor
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
@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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
@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
3963
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 when leaving it). It seems to be impossible to delete a record in this subform. When I switched modal off and tried to delete a record from the list, I deleted a record on another form (below the popup form).
8
12084
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. Cuurrent record should be the same as the value in the combobox. What is the solution? Thank you in advance.
3
11806
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 found most of the codes from the internet and adjusted some part of it. Anyhow, inserting records to the database seems to work fine, but deleting the records is not working as I want to and I just can't see what's causing the problem.
6
1767
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 night. Now the button on the form to delete a record doesn't work. i get a "compile error: method or data member not found". The code for the drop-down is this: Private Sub REC_ID_COMBO_AfterUpdate()
1
3310
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 Current record in both form & Table. I used the following code to delete. --- Dim response Dim strsql As String response = MsgBox("Are you sure to Delete?", vbYesNo, "Payment Alteratiion") If response = vbYes Then
8
1388
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 button for that specific record, my form goes to the next record and ask the user "if they want to delete the current record" if the user says "yes" then the previous record gets deleted. I want them to see the record before they delete something. Also, if the user say NO. the MS message box come up...
5
2473
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 main_text which gets it's values by pulling the selected data from the database. If the user either i) doesn't change anything, and then saves. or
7
3299
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 do I delete the current main form record if the sub form is not filled?
3
8273
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
2235
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 bizarre issue. Here is the relevant code. Pressing the button triggers the btnDelete_Click() subroutine, which calls DeleteRecord() which in turn can call MoveLine(). Private Sub btnDelete_Click() L = txtLine.Value If Form.NewRecord Then 'The current record has never been saved, so...
0
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.