0

I have a users.php file where I store all the functions. I want to create a remove button to remove any of the listed employees under a certain manager. Here is my view of members function:

public function view_team($username) {
    $query = $this->db->prepare("SELECT * FROM `users` WHERE `manager`= ? ORDER BY `lastname` ASC ");
    $query->bindValue(1, $username);
    $i = 0;

    try{
            $query->execute();
            foreach ($query as $row) {
                $removable=$row['id'];
                print $i+1 .') '.$row['lastname'].' '.$row['firstname'].', Department: '. $row['department'] .', Band: '.$row['band'].' | '; ?> 
                Promote  <img src="img/promote.png" alt="Promote employee"> | <!-- promote user -->
                Edit  <img src="img/edit.png" alt="Edit employee"> | <!-- edit user -->
                <a href ="#" onclick="$users->remove($removable)">
                Remove <img src="img/remove.png" alt="Remove employee"></a> <!-- remove user -->
                <?php print '<br>';
                $i++;
            }

    $this->queryResult = $query->fetch();

    } catch(PDOException $e){
        die($e->getMessage());
    }
}   

Here is the remove function:

public function remove($username) {
    $query=$this->db->prepare("DELETE from `users` WHERE `id`=?"); 
    $query->bindValue(1,$removable);
    try{
        $query->execute();
        } catch (PDOException $e){
        die($e->getMessage());
        }
}

This is how I'm viewing the users:

<?php
$usr = $firstname ." ". $lastname;
$users->view_team($usr);

?>

I think I need to create an onclick() function to work only when Remove button is pressed.

Cataneo
  • 155
  • 1
  • 2
  • 12

1 Answers1

0

Your removing code starts automaticly, not manual:

<?php 
                        $removable = $row['id'];
                        $query=$this->db->prepare("DELETE from `users` WHERE `id`=?"); 
                        $query->bindValue(1,$removable);
                        try{
                            $query->execute();
                        } catch (PDOException $e){
                            die($e->getMessage());
                        }
                    ?>

After edited:

Please try;

<a href ="#" onclick="$users->remove($removable)" ?> 
"> Remove <img src="img/remove.png" alt="Remove employee"></a>

ınstead of;

<a href =" 
   <?php $removable=$row['id']; $users->remove($removable); ?> 
">Remove <img src="img/remove.png" alt="Remove employee"></a>
  • Yes, I noticed that. My actual question is how do I trigger it only when button is pressed. – Cataneo Nov 05 '13 at 11:40
  • 2
    First; if you want stay in this page when clicking on image, you should use javascript. 1- Create your new php page(for example delete.php) and write your delete actions in this page. 2- Create your javascript function in this page which is have `view_team()`... For example `delete()`... This function must have POST type for sending your data to `delete.php` 3- For run javascript function (`delete()`) you must write `onclick="delete()"` in your object(image or button). ..... It can helps you: http://stackoverflow.com/questions/16313518/updating-database-from-javascript-by-calling-php-script – Orbay Yeşil Nov 05 '13 at 11:58
  • I modified the function and created a different function for remove. I will update the whole thing. – Cataneo Nov 05 '13 at 12:02
  • I don't seem to find a way to make it work. I had to modify your entry a little. Please check my update. – Cataneo Nov 05 '13 at 12:36
  • Ok. Please first try `` instead of your image. İf it will works, modify your image's `onclick`. If it will not works, I can't help you anymore. Because my knowledge is not enough for anymore. – Orbay Yeşil Nov 05 '13 at 12:44