Removing DB Entires

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Removing DB Entires

Post by volkan »

Hi
I have a table, with a number on entries that must be manually deleted from the database.
What is the script for removing rows (entries) from a mysql table?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

this is generally done like so..

1. you have have a unique ID on each row.. this can be accomplished by having a column (ID) set to autoincrement

2. You loop through your results, and create the links, adding the unique id on the link

Code: Select all

$result = mysql_query("SELECT * FROM `rows`") or die(mysql_error());

while ($row = mysql_fetch_assoc($result))
{
     echo '<a href="delete.php?id='.$row&#1111;'id'].'">Delete ME</a>";
&#125;
and then on delete.php you would have something like this

Code: Select all

if (!empty($_GET&#1111;'id']))
&#123;
     mysql_query("DELETE FROM `rows` WHERE `id` = '".$_GET&#1111;'id'].' LIMIT 1") or die(mysql_error());
&#125;
for more information and syntax on mysql visit

http://dev.mysql.com/doc/mysql/en/index.html
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

Thanks for the reply.
Do i need to include a dbconnent.php?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

volkan wrote:Thanks for the reply.
Do i need to include a dbconnent.php?
Likely, as you are connecting to the database prior to retrieving/deleting data.

Additionally, you do not crearly (for me ;)) mention if you are deleting some or all data in the table. If all, you could look at the TRUNCATE function.
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

Hi
I am trying to remove all the entires on the table!
That link doesn't contain the script for it.
What is the line?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

volkan wrote:Hi
I am trying to remove all the entires on the table!
That link doesn't contain the script for it.
What is the line?
:roll: You should try to research your work before actually asking that, as it might be of critical interest of understanding these malicious functions...

Code: Select all

mysql_query('truncate table table_name');
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

Hi,
Sorry, i admit im impatient.
So....
Is this corrrect??:

REMOVE.php

Code: Select all

<?php
$result = mysql_query("SELECT * FROM `rows`") or die(mysql_error()); 

while ($row = mysql_fetch_assoc($result)) 
&#123; 
     echo '<a href="delete.php">Delete ME</a>"; 
&#125; 
?>
and

delete.php

Code: Select all

<?php
include("DBCONNECT.php");
&#123; 
     mysql_query('truncate table table_name') or die(mysql_error()); 
&#125;
?>

&#123; 
     mysql_query('truncate table table_name') or die(mysql_error()); 
&#125;
?>
also,
what is a good book to learn about php + mysql insterface?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

MySQL Manual wrote:13.1.9. TRUNCATE Syntax

TRUNCATE TABLE tbl_name

TRUNCATE TABLE empties a table completely. Logically, this is equivalent to a DELETE statement that deletes all rows, but there are practical differences under some circumstances.
Meaning, if you issue this command, you will empty the entire table. Links containing 'Delete ME' as in your REMOVE.php file are obsolete.
You can asweel link to/visit the delete.php directly ( http://www.example.com/delete.php ) and the data will be gone...

The delete.php only needs the truncate table query one time aswell.
User avatar
volkan
Forum Commoner
Posts: 60
Joined: Sat Aug 21, 2004 1:12 pm
Location: London, United Kingdom

Post by volkan »

i got ya,
so accessing delete.php
which contains:

Code: Select all

<?php 
include("DBCONNECT.php"); 
&#123; 
     mysql_query('truncate table table_name') or die(mysql_error()); 
&#125; 
?>
that only deletes the entires right? no the sql structure.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

MYSQL manual, PHP Manual and virtually any PHP book out there explain all this in great detail. Maybe it's time you did some work on this. Google can help you for sure. If in doubt, click the second link in my sig.

Topic locked.
Locked