Forums | MacLife

You are not logged in.

#1 2008-02-01 2:53 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 482
Website

programmatically deleting user records in a mysql database - php/mysql

i want this script to delete records in a database for any occurrence of a client id (regID). ideally, i'd also like the script to be flexible enough that it would be able to perform this task even if more tables were added to the database and they contained the regID data (with a column named regID).

here's my code ....

Code:

<?php


        $post_values = array_values($_POST);
        $post_keys = array_keys($_POST);
        $post_count = count($_POST);
            
        
        
        for($y=0; $y<$post_count; $y++){
        
            if(is_array($post_values[$y]) && $post_keys[$y] == 'myCheckbox'){
            
                $check_box_array = $post_values[$y];
                        
            }
        
        }
        
            
        $checkbox_count = count($check_box_array);
        
        
        // connect to DB
        $link = mysql_connect('localhost', 'username', 'password');
        
        mysql_select_db('databsename', $link);
        
        // retrieve all table names in DB
        $table_query = mysql_query("SHOW TABLES FROM databsename") or die(mysql_error());
        
        
        while($row = mysql_fetch_array($table_query, MYSQL_ASSOC)){
                    
            mysql_select_db('databsename', $link);
            $field_name_query = mysql_query("SELECT * FROM ".$row['Tables_in_databsename']."") or die(mysql_error());
            $num_fields = mysql_num_fields($field_name_query);
        
                for($z=0; $z<$num_fields; $z++){
                
                    // create FIELD NAME array for each table
                    $field_name_array[] = mysql_field_name($field_name_query, $z);
            
                } // end field name loop for TABLE            
            
            
            // check for regID in FIELD NAME array
            
            if(is_array($field_name_array) && in_array('regID',$field_name_array)){
            
                
                        for($x=0; $x<$checkbox_count; $x++){
                
                        
                                    $regid_sql = "SELECT * FROM ".$row['Tables_in_databsename']." WHERE regid=".$check_box_array[$x]."";
                                    
                                    
                                     
                                    mysql_select_db('databsename', $link);
                                    $regid_query = mysql_query($regid_sql);
                                    $regid_result = mysql_fetch_assoc($regid_query);
                                    $num_regid_results = mysql_num_rows($regid_query);
                                    
                        
                                    
                                    
                                    if($num_regid_results > 0){
                                    
                                        mysql_select_db('databsename', $link);
                                        $delete_regid = "DELETE FROM ".$row['Tables_in_databsename']." WHERE ".$row['Tables_in_databsename'].".regID = ".$regid_result['regID']."";

                                        $delete_regid_query = mysql_query($delete_regid) or die(mysql_error));
                                        
                                    
                                    } // end if            
                                    
                                    
                                    
                        } // end FOR each checkbox value in tablename
                        
                
            } // end if regID in FIELD NAME array
            
            
            
            
            
            unset($field_name_array);
        
        }

        
        
        
        mysql_close($link);
        

        
        
?>

the values i'm using from the form post is an array (from checkboxes) that are set to the value of the regID.

my DELETE statement is bombing and i'm wondering if i've missed something somewhere (obviously) or if my logic is flawed somewhere.  the idea was to loop through tablenames, test for regID column name in each table, and then loop through the array of regID values.

thanks in advance to anyone that lends a hand

b

Last edited by b_dubb (2008-02-01 2:59 pm)


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

#2 2008-02-01 3:35 pm

Alien
FF
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16573
Website

Re: programmatically deleting user records in a mysql database - php/mysql

Why do you need to loop through all of your tables to see if they contain regID? Why not use referential integrity and cascade delete to get rid of data belonging to that one regID?

,xtG
.tsooJ

Offline

 

#3 2008-02-01 4:46 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 482
Website

Re: programmatically deleting user records in a mysql database - php/mysql

cause i'm not familiar with either of those items.  what would that look like?


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

#4 2008-02-01 4:51 pm

Alien
FF
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16573
Website

Re: programmatically deleting user records in a mysql database - php/mysql

Code:

delete from users where regid = $regid

,xtG
.tsooJ

Offline

 

#5 2008-02-01 5:11 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 482
Website

Re: programmatically deleting user records in a mysql database - php/mysql

i'm not following you. 

part of the reason for looping through the tables was that there's always the chance that new tables will be added that will contain regID column and values.  my approach would accomodate that possibility.

thanks.

b


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

#6 2008-02-01 7:17 pm

Miles
Now I fight for wisdom!
Administrator
From: Michigan
Registered: 2001-07-21
Posts: 4480
Website

Re: programmatically deleting user records in a mysql database - php/mysql

If new tables were added that used regID as a foreign ID, a single cascading delete would still take care of them.

Offline

 

#7 2008-02-04 12:52 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 482
Website

Re: programmatically deleting user records in a mysql database - php/mysql

i neglect to enter a ( in the delete query line ...

(mysql_error))

should be ...

(mysql_error())

oops

its working now though

thanks to everyone who tried to help

b

Last edited by b_dubb (2008-02-04 1:28 pm)


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

#8 2008-02-04 6:08 pm

Alien
FF
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16573
Website

Re: programmatically deleting user records in a mysql database - php/mysql

You should still educate yourself on database design.

,xtG
.tsooJ

Offline

 

#9 2008-02-04 7:16 pm

cosmicosmo
Chancellor Mmmm
From: Peninsula, Ohio, US of A
Registered: 2003-02-06
Posts: 236
Website

Re: programmatically deleting user records in a mysql database - php/mysql

Tables must be of InnoDB type for the referential integrity constraints to work.


I hate your wimper

Offline

 

#10 2008-02-05 12:47 pm

b_dubb
loch whatchamacallit
From: chapel hill, nc
Registered: 2002-11-19
Posts: 482
Website

Re: programmatically deleting user records in a mysql database - php/mysql

in my defense ... i have inherited someone else's web app.  the database tables are all myisam.  so the DELETE CASCADE won't work.  but you're right.  ongoing education is necessary/crucial.

b


"The Fates lead he who will; he who won't, they drag." - Seneca

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson