I've been trying to figure out why my database won't update! I know i'm doing something wrong but I don't know what's wrong! here's my code! please help.
$user_id = (int)$user['id'];
$editName = $user['full_name'];
$editEmail = $user['email'];
$editPermissions = $user['permissions'];
if(isset($_POST['editUser'])){
$editName = $_POST['editname'];
$editEmail = $_POST['editemail'];
$editPermissions = $_POST['editpermissions'];
$db->query("UPDATE users SET full_name = '$editName',email = '$editEmail', permissions = '$editPermissions' WHERE id = '$user_id'");
}
?>
<form action="users.php" method="post">
<td><input type="text" name="editname" id="editname" class="form-control col-sm-2" value="<?=$editName;?>"></td>
<td><input type="text" name="editemail" id="editemail" class="form-control col-sm-2" value="<?=$editEmail;?>"></td>
<td><?=pretty_date($user['join_date']);?></td>
<td><?=(($user['last_login'] == '0000-00-00 00:00:00')?'never':pretty_date($user['last_login']));?></td>
<td><input type="text" name="editpermissions" id="editpermissions" class="form-control col-sm-2"
value="<?=$editPermissions;?>"></td>
<div class="form-group col-md-6 text-right button">
<a href="users.php" class="btn btn-default">Cancel</a>
<input type="submit" name="editUser" value="Edit User" class="btn btn-success">
</div>
</form>
It looks like you're missing the id of the user in the form.
Add
<input type="hidden" name="id" value="<?= $user_id ?>" />
Add this to your second if(){}
$user_id = $_POST['id'];
As a side note, you should not be using <td>
in a <form>
if they're not in a <tr>
and the non-existant <tr>
is not in a <table>
. Have a look here
Next up, you're <input>
's also do not have a <label>
(matched together using for
and id
attributes). Look at this
Last up, you're <input type="submit" name="editUser" value="Edit User" class="btn btn-success">
could be replaced using a <button>
element. As you're using Bootstrap you could find a proper submit button here
Lastly, the way the code is written is pretty unreadable. Please read this , commit it to memory and use it ;)
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.