[已解决]
我有一个update.php文件来更改某些用户角色。我得到消息更新记录成功,但是数据库表中没有任何更改。可以向我指出我哪里做错了。我是Php的新手。
更新特定用户的用户名和user_role。
update.php
<?php
require 'dbconnect.php';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$newUsername = mysqli_real_escape_string($conn, $_POST['username']);
$newRole = mysqli_real_escape_string($conn, $_POST['role-dropdown']);
$id = $_POST['id'];
$sql = "UPDATE users SET username = '$newUsername', user_role = '$newRole' WHERE userID = '$id';";
if($conn->query($sql) === TRUE)
{
echo("Record updates successfully");
}
else
{
echo("Error updating record: ");
die(mysqli_error($conn));
}
}
$conn->close();
?>
具有更改用户名和用户角色的表单的网页
edit_user.php
<main>
<div id="gt-menu">
</div>
<form id="user-form" class="form" method="POST" action="includes/update.php">
<h3>Edit User Role</h3>
<label for="username">Username:</label>
<input id="username" type="text" name="username" value="<?php echo $row['username']; ?>">
<input type="hidden" name="id" value="<?php echo $row['userID']; ?>">
<select class="select" id="role-dropdown" name="role-dropdown">
<option value="_Any" selected="selected">Please Choose</option>
<option value="1">Admin</option>
<option value="2">Manager</option>
<option value="3">General User</option>
<option value="4">External Company</option>
</select>
<button type="submit" name="update-btn" id="update-btn">Update</button>
</form>
</main>
答案 0 :(得分:2)
您的ID没有值
<input type="hidden" name="id">
应该是这样的:
<input type="hidden" name="id" value="ID_YOU_WANT_TO_UPDATE">