473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 12772
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 when leaving it). It seems to be impossible to...
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. Cuurrent record should be the same as the value in the...
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 found most of the codes from the internet and...
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 night. Now the button on the form to delete a record...
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 Current record in both form & Table. I used the...
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 button for that specific record, my form goes to the...
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 main_text which gets it's values by pulling the selected...
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 do I delete the current main form record if the...
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 bizarre issue. Here is the relevant code....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.