I am building a task manager with projects and departments. Each project has somedepartments. In the admin interface one can add a department. Of course you have to assign it to some projects (yes, more than one is possible).

So this would make up something like this to check the checkboxes and insert the department to the projects:

PHP Code:
            //-------------------------------------------
            // Insert the Department into the DB
            //-------------------------------------------
                    
            
if( isset( $SITE->input['projects'] ) )
            {                    
                foreach( 
$SITE->input['projects'] as $projId )
                {    
                    
$DB->query"INSERT INTO intr_departments (name, proj_id) VALUES ('{$SITE->input['deptName']}', '{$projId}')" );
                }
            }
            
            
$this->content .= 'Department successfully added.'
So far so good. But I have another feature in the admin interface: You can add assign a user to some projects, to some departments, etc. All you have to do is pick a name and click some checkboxes to assign him to certain projects and departments. Looks similar to this:

PHP Code:
                //-------------------------------------------
                // Departments
                //-------------------------------------------
                
                
if( isset( $SITE->input['departments'] ) )
                {
                      
$DB->query"DELETE FROM intr_user2department WHERE user_id = '{$SITE->input['userId']}'" );
                      
                    foreach( 
$SITE->input['departments'] as $deptId )
                    {                      
                        
//-------------------------------------------
                        // Did we determine user should be a dept
                        // leader? :)
                        //-------------------------------------------
                            
                        
$isDeptLead 0;
                            
                        if( isset( 
$SITE->input["departments_Leader::{$deptId}"] ) ) 
                            
$isDeptLead $SITE->input["departments_Leader::{$deptId}"];
                            
                            
                        
//-------------------------------------------
                        // Insert the department into the collection 
                        // table
                        //-------------------------------------------
                            
                        
$DB->query"INSERT INTO intr_user2department (user_id, dept_id, is_dept_lead) 
                                        VALUES ('
{$SITE->input['userId']}', '{$deptId}', '{$isDeptLead}')" );
                    }
                } 
Hmm. Some of the code looks exactly like the one from the first code box. Is this refactorable ? Must this be refactored ? Is this a violation of the DRY (Don't-Repeat-Yourself)-Principle?

What do you think ?