简体   繁体   中英

Delete user with button and setup profile page PDO

Started learning a little PDO now after a few suggestion on sites to look at from helpfull users here. I'm still fresh, so i dont want the full solution, but a point in the right direction since i am a little unclear on what to type in.

I have two problems that i have been trying to figure out here. Have been searching google, watching videos and reading ALOT of posts in here to see if i could find a solution. But since there are many ways to set up everything, it just gets a little "loco" in my head with different solutions and different opinions.

Well. Back on track.

    <div class="row">
    <div class="col-xs-12 col-md-12 user_group">
    <h2 class="text-center">Aktive brukere:</h2>
        <?php 
                $stmt = $DB_con->query('SELECT * FROM users ');
                $stmt->execute();


                echo "<table id='user' class='table table-bordered table-striped'>
                    <tr>
                    <th>Bruker-ID</th>
                    <th>Brukernavn</th>
                    <th>E-post</th>
                    <th>Fornavn</th>
                    <th>Etternavn</th>
                    <th>Rolle</th>
                    <th>Banna</th>
                    <th>Deaktivert</th>
                    <th>Profil</th>
                    <th>Slett</th>
                    </tr>";


                while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                {

                    $userID = $row['user_id'];
                    $username = $row['user_name'];
                    $email = $row['user_email'];
                    $firstname = $row['user_fname'];
                    $lastname = $row['user_lname'];
                    $role = $row['user_role'];
                    $banned = $row['user_banned'];
                    $deactivated = $row['user_deactivated'];


                    {
                        echo "<tr>
                                <td>$userID</td>
                                <td>$username</td>
                                <td>$email</td>
                                <td>$firstname</td>
                                <td>$lastname</td>
                                <td>$role</td>
                                <td>$banned</td>
                                <td>$deactivated</td>
                                <td><button class='btn btn-block btn-primary'><a href='../inc/profile.php'></a>Profil</button></td>
                                <td><button class='btn btn-block btn-danger'><a href='process.php?userID=". $userID ."'></a>Slett</button></td>";
                    }

                }
            ?>

    </div>
</div>

This is my list of active users on the site. I just have created a few test users here to test everything out.

I have two buttons. Profile and Delete. I guess i have to create 2 more files called profile.php and delete.php, but what i should have in those files are kinda, well very unclear to me.

I need to get the ID from the user where the button is, but here is my problem. I cant seem to get the user i want.

Several tries i had gave me every user on the list or just me as logged in user. It looks like i dont need much code in these files to get the user ID and get the right user deleted.

If i could just get a little pointer on how i delete a user with my button, i can figure out the profile part, since it's getting the ID from the user clicked that seems to be the problem.

Sorry for my english and long post.

PS: This is my user.php class script if it could help:

    class USER
{
    private $db;

    function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function register($fname, $lname, $uname, $umail, $upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);

            $stmt = $this->db->prepare("INSERT INTO users(user_name,user_email,user_pass,user_fname,user_lname) VALUES(:uname, :umail, :upass, :fname, :lname)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);
            $stmt->bindparam(":fname", $fname);
            $stmt->bindparam(":lname", $lname);
            $stmt->execute();

            return $stmt;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

i use the following code and it always works for me.

$sql="SELECT * FROM users";
foreach($conn->query($sql) as $row){
    echo '<a href="delete-user.php?id='.$row['user_id'].'">delete</a>';
}

then on delete-user.php page

$query=$conn->prepare("DELETE FROM users WHERE id=:id");
$query->execute(array(':id'=>$_GET['id']));

Since you don't want a full solution and I totally respect that. It's the best way to learn. I'm gonna give you some clues.

If you want to delete an user you need to create a link just like

<a href='process.php?userID=". $userID ."'></a>

but change process.php to different file that will take care of deleting user. In that file you need to execute DELETE query the same way you are executing INSERT query in user.php, you will need only one param "id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM