0

Good Afternoon and Thanks in advance for any help you can provide. I am trying to create an adjust Role for my login system with clicking on a button.. i.e promote to admin but I can't for the life of me get this to work.

I have tried a few tutorials as new to php and have my table showing on screen but sadly the buttons don't appear to update anything.

I have added the code i have and hope this helps as i am somewhat confused. I don't mind losing Symfony if this can make it more simple.

Thanks in advance.

PHP code:

$userId = ['id'];
$role = ['role_id'];

switch (strtolower($role)) {
  case 'promote':
    promote($userId);
    $session - > getFlashBag() - > add('success', "Promoted to Admin!");
  case 'demote':
    demote($userId);
    $session - > getFlashBag() - > add('success', "Demoted from Admin!");
}
header('Location: /admin.php');
exit();

JavaScript:

function getAllUsers() {
    global $db;

    try {
        $query = "SELECT * FROM users";
        $stmt = $db->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);

    } catch (\Exception $e) {
        throw $e;
    }
}

function promote($userId) {
    global $db;

    try {
        $query = "UPDATE users SET role_id=1 WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->bindparam(1,$userId);
        $stmt->execute();
    } catch (\Exception $e) {
        throw $e;
    }
}
function demote($userId) {
    global $db;

    try {
        $query = "UPDATE users SET role_id=2 WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->bindparam(1,$userId);
        $stmt->execute();
    } catch (\Exception $e) {
        throw $e;
    }
}
function superuser($user_id) {
    global $db;

    try {
        $query = "UPDATE users SET role_id=3 WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->bindparam(1,$user_id);
        $stmt->execute();
    } catch (\Exception $e) {
        throw $e;
    }
}

function request() {
    return \Symfony\component\HttpFoundaton\Request::createFromGlobals();
}

HTML:

<div class="container">
  <div class="well">
    <h2>Admin</h2>

    <div class="panel">
      <h4>Users</h4>
      <table class="table table-striped">
        <thead>
          <tr>
            <th>Username</th>
            <th>Registered</th>
            <th>Promote/Demote</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach (getAllUsers() as $user): ?>
          <tr>
            <td>
              <?php echo $user['username']; ?>
            </td>
            <td>
              <?php echo $user['join_date']; ?>
            </td>
            <td>
              <?php if ($user['role_id'] == 1): ?>
              <a href="/partials/adjustRole.php?role=demote&userId=<?php echo 
                        $user['id']; ?>" class="btn btn-sm btn-warning" name="demote">Demote from Admin</a>
              <?php elseif ($user['role_id'] == 2): ?>
              <a href="/partials/adjustRole.php?role=promote&userId=<?php echo 
                        $user['id']; ?>" class="btn btn-sm btn-success" name="admin">Promote to Admin</a>
              <?php elseif ($user['role_id'] == 3): ?>
              <a href="/partials/adjustRole.php?role=promote&userId=<?php echo 
                        $user['id']; ?>" class="btn btn-sm btn-info" name="superuser">Promote to SuperUser</a>
              <?php endif ?>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
3
  • Please don't post snippets containing PHP. It also had JS code in the CSS section Commented Jan 17, 2020 at 11:48
  • Sorry.. How do i go about posting all of the PHP? Commented Jan 17, 2020 at 11:54
  • 1
    Your code makes little sense right from the start. $role = ['role_id']; switch (strtolower($role)) { - you made $role an array, so calling strtolower on it is just wrong, and using switch on an array also makes rather little sense. Commented Jan 17, 2020 at 12:04

1 Answer 1

1

Congratulations for making that functions work (if they work).Probably not.

This function wont work because of bindParam expecting 1 parameter but you send 2 parameters.

Other than that if you are creating new function each time, then dont need a function, you can just create a normal query and update your table.

Using functions means it should save you time from writing more codes, do more work with less code.

So function should be like this in your case :

$userId = ['id'];
$role = ['role_id'];

function demote($userId) {
    global $db;

    try {
        $query = "UPDATE users SET role_id = ? WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->bindparam(1,$userId);
        $stmt->bindparam(2,$role);
        $stmt->execute();
    } catch (\Exception $e) {
        throw $e;
    }
}

In my case it should be like this without bindparams:

function update($sql, $params) {
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    return $stmt; 
}

And usaqe :

$sql = "UPDATE users SET role_id = ? WHERE id = ?";
update($sql, array($role, $userId));

NOTE: I dont use symfony

See difference between your function and mine : how to call a function multiple times with where clause in php pdo?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help :), i will give this a go once home and see if this fixes my issue. Thank you for your time :)
@SteveMeadows it will with right setup good luck :) and if it works please accept answer as true :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.