php - Execute query update with form from a query -
i trying create button on user list page delete row, or make user admin.
here info user query , html:
<?php $query = "select * users"; try { $stmt = $db->prepare($query); $result = $stmt->execute(); } catch(pdoexception $ex) { die("an error has occured. please contact server administrator assistance."); } $rows = $stmt->fetchall(); ?> <?php foreach($rows $row) : ?> <?php if($row['usertype'] == 2) { $usertype = "<span style='color:#f7fe2e;'>donator</span>"; } elseif($row['usertype'] == 3) { $usertype = "<span style='color:red;'>admin</span>"; } elseif($row['usertype'] == 4) { $usertype = "<span style='color:orange;'>owner</span>"; } else { $usertype = "<span style='color:#585858;'>normal</span>"; } ?> <tr> <!--<td><?php echo $row['id']; ?></td>--> <td><?php echo htmlentities($row['username'], ent_quotes, 'utf-8');?></td> <!--<td><?php echo htmlentities($row['email'], ent_quotes, 'utf-8');?></td>--> <td><?php echo htmlentities($row['steamid'], ent_quotes, 'utf-8');?></td> <td><?php echo $usertype?></td> <td><form action="" method="post"> <input type="submit" name="admin" value="promote" /> </form></td> </tr> <?php endforeach; ?>
and code prepare , execute update query:
if(!empty($_post['admin'])) { $query = "update `users` set `usertype` = '3' `id` = " . $row['id']; // $query_params = array(':id' => $row['id']); try { $stmt = $db->prepare($query); $result = $stmt->execute(); } catch(pdoexception $ex) { die("an error has occured. please contact server administrator assistance."); } }
unfortunately when run current setup, updates last row. further ask looking for, have list of users: "admin_b" button forced $_post['admin']
- billy
admin_b
- bob
admin_b
- jill
admin_b
- jack
admin_b
update:
so in form have input <input type="hidden" name="id" value="<?php $row['id']; ?>" />
, added sql $query = "update users set usertype = '3' id = :id";
$query_params = array(':id' => $_post['id']);
try { $stmt = $db->prepare($query); $result = $stmt->execute(); } catch(pdoexception $ex) { die("an error has occured. please contact server administrator assistance."); }
send id $_post request, update user id = $row['id']
where `id` = " . $row['id'];
Comments
Post a Comment