Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 05-23-2007, 10:19 AM   #1 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Records using an Array

Howdy Everyone,

I'm currently using the following, in order to delete records in a database upon clicking a delete button:

Code:
<? $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } $view=$_GET["view"]; $id=$_GET["id"]; switch ($view) { case "list": mysql_select_db("WB", $con); $result = mysql_query("SELECT * FROM Birds"); echo "<table border='1'> <tr> <th>Name</th> <th>Species</th> <th>Sex</th> <th>Delete</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Name'] . "</td>"; echo "<td>" . $row['Species'] . "</td>"; echo "<td>" . $row['Sex'] . "</td>"; echo "<td><input type=\"button\" value=\"Delete\" onClick=\"parent.location='Select.php?view=delete&id=" . $row['ID'] . "'\"></td>"; echo "</tr>"; } echo "</table>"; break; case "delete": //insert your mysql string here deleting the record. mysql_select_db("WB", $con); mysql_query("DELETE FROM Birds WHERE id='$id'"); mysql_close($con); //use the header function to get you back to the previous screen. $url="Select.php?view=list"; header("Location: $url"); break; default: $url="Select.php?view=list"; header("Location: $url"); } ?>
Instead of using a static ID to identify a record, is there anyway of calculating the number of records in the table, and using the record number as the ID in order to delete a record.

Currently, I'm using a delete button next to each button, to delete the appropriate record, how can one delete button be used along with check boxes?

Many Thanks, much appreciated!!!
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-25-2007, 05:29 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
not a good idea in my opinion.. if a record got deleted by user 1 between the time that the page loaded for user 2 and when user 2 presses delete, then the numbers would be off.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 05-25-2007, 11:07 PM   #3 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Records using an Array

SDE,

So, how do records in a database usually get deleted? As the ID's for each record are going to have massive gaps in between the other records.

For example you would have the following if record ID 2,5,8 got deleted.

ID
1
3
4
6
7
9
10

How can this be stopped?

Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-27-2007, 05:38 PM   #4 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally Posted by Salchester View Post
So, how do records in a database usually get deleted? As the ID's for each record are going to have massive gaps in between the other records.

For example you would have the following if record ID 2,5,8 got deleted.

ID
1
3
4
6
7
9
10

How can this be stopped?

Many Thanks,
It can't and it shouldn't. MySQL keeps track of the records by it's own method using your ID value. It doesn't matter if the ID values are in a nice neat sequential list.
__________________
bdl is offline   Reply With Quote
Old 05-28-2007, 11:11 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
yeah, the purpose of an id field is just so that each record has a unique piece of data you can identify it with.

as you pointed out, the id's will have gaps when you delete them, but why do you need to number them?

is it only so you can show the users a nicely numbered list? if that is the case, then you should just number it when you print out the data to the user.. you don't have to use a database field for that.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 05-29-2007, 02:43 AM   #6 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Records using an Array

SDE,

So I don't need IDs?
How will the single delete button know which records have a check box next to them, in order to delete the appropriate record.

How can this be done?

Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-29-2007, 06:50 AM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
Quote:
So I don't need IDs?
i didn't say that.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 05-29-2007, 08:33 AM   #8 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Records using an Array

Quote:
Originally Posted by sde View Post
yeah, the purpose of an id field is just so that each record has a unique piece of data you can identify it with.

as you pointed out, the id's will have gaps when you delete them, but why do you need to number them?

is it only so you can show the users a nicely numbered list? if that is the case, then you should just number it when you print out the data to the user.. you don't have to use a database field for that.
SDE,

As you mentioned above, why do I need to number them?
I'm not numbering the records to output a numbered list, so why do I need the ID's?

I'm now confused, so do I need the ID's now? :-)
But how will the code now which records to delete?

Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-29-2007, 10:19 AM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
ah ok, well since you were concerned about the IDs being in order.. i was guessing that you also wanted to number them... otherwise, who in the world would care if the IDs have gaps in them.

forget about the numbering then.. that was an example that doesn't fit your needs.

as far as creating a link to delete the file, it looks like you already have the code to do that. your link ends with ?id=...

did you write that code or did you just find the script somewhere?
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 05-29-2007, 11:22 AM   #10 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Records using an Array

SDE,

Sorry, I think I'm confusing my-self, and you for that matter.

Currently I have a table that adds an ascending number to a ID column in a database, the problem is when a user deletes a record, gaps in the numbering are left.

Can the ID be set depending on the total amount of records stored in the database? Upon a record being deleted, the ID's are replaced with new ID's totaling the amount of records currently stored in the database.

For example, f i had ten records and deleted 4, the ID's for the remaining records will need to be replace with number 1-6.

Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-29-2007, 01:39 PM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
Quote:
the problem is when a user deletes a record, gaps in the numbering are left.
please give me one reason why you think this is a problem? it is absolutely not a problem and this practice is probably used in nearly all db designs.

i think you just need to know that it is OK to have gaps in numbers that represent an ID field.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 05-29-2007, 01:48 PM   #12 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Delete Record using an Array

SDE,

No, you see I will being using the IDs later to carry out other functions, therefore I need the record IDs to correspond with the amount of records there are in the database.

Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-29-2007, 01:50 PM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
mysql has functions for that.

Code:
$result = mysql_query("select count(*) from my_table"); $number_of_results = mysql_result($result, 0, 0);
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 05-29-2007, 01:57 PM   #14 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
SDE,

So how do I delete record 2 from the total amount of records.
Assuming there is a record 2 in the table, of course.

Many Thanks,
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 05-29-2007, 02:20 PM   #15 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
when you say 'record 2' .. do you mean the record with the ID 2? .. or do you mean the 2nd record in the table?
__________________
testing 1 2 3
sde is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with array Michael12 Java 3 05-01-2007 11:53 PM
creating 3-dimensional array nesvarbu Platform/API C++ 3 05-09-2006 07:01 AM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
delete parts of an array Apodysophilia Java 4 05-07-2003 12:11 PM


All times are GMT -8. The time now is 06:41 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle