简体   繁体   中英

How to have a Button update a mysql database in php

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.. ie 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>

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?

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