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>
$role = ['role_id']; switch (strtolower($role)) {- you made$rolean array, so callingstrtoloweron it is just wrong, and usingswitchon an array also makes rather little sense.