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. -
<?
-
//connect to mysql
-
//change user and password to your mySQL name and password
-
mysql_connect("localhost","root","root");
-
-
//select which database you want to edit
-
mysql_select_db("mydb");
-
-
//If cmd has not been initialized
-
if(!isset($cmd))
-
{
-
//display all the news
-
$result = mysql_query("select * from stuinfo order by FirstName");
-
-
//run the while loop that grabs all the news scripts
-
while($r=mysql_fetch_array($result))
-
{
-
//grab the title and the ID of the news
-
//$title=$r["title"];//take out the title
-
$FirstName=$r["FirstName"];//take out the id
-
-
//make the title a link
-
echo "<a href='delete.php?cmd=delete&FirstName=$FirstName'></a>";
-
echo "<br>";
-
}
-
}
-
?>
-
-
<HTML>
-
<HEAD>
-
<TITLE>New Document</TITLE>
-
</HEAD>
-
<BODY>
-
<? php
-
-
if($_GET["cmd"]=="delete")
-
{
-
$sql = "DELETE FROM stuinfo WHERE FirstName=$FirstName";
-
$result = mysql_query($sql);
-
echo "Row deleted!";
-
}
-
?>
-
</BODY>
-
</HTML>
-
6 12803
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
What you'd probably want is a record id included with the list of displayed rows.
Here's a simple example of one approach: -
?php
-
// Delete rows test
-
if (isset($_POST['submit'])) {
-
$sql = 'delete from table_x where id in (';
-
foreach ($_POST['id'] as $a) {
-
$sql .= $a . ',';
-
}
-
$sql = substr($sql,0,strripos($sql,',')) . ')';
-
print $sql;
-
}
-
?>
-
<html>
-
<head><title>Test deleting records</title></head>
-
<body>
-
<form action="" method="post">
-
<table border="1">
-
<thead><th>Delete?</th><th>Record Detail</th></thead>
-
<?php
-
for ($i=0;$i<10;$i++) {
-
// Note the variable name id[] is an array...
-
print '<tr><td><input type="checkbox" name="id[]" value="'.$i.'" /></td>';
-
print "<td><p>Record#$i</p></td></tr>";
-
}
-
?>
-
</table>
-
<input type="submit" name="submit" value="Delete Record(s)!" />
-
</form>
-
</body>
-
</html>
-
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.
@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....
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.
@kovik
Yep... Much better... And yep... that's the normal way of doing it...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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).
|
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.
|
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.
|
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()
|
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
| |
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...
|
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
|
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?
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |