我已经搜索了我的问题的解决方案,但到目前为止未能找到一个有效的解决方案。
我正在尝试使用PHP更新MySQL表,但我不断收到错误未定义索引:userid。
以下是我的editUsers.php页面的所有代码。谁能看到我错在哪里?
confirm_is_admin();
$sessionid = $_SESSION['userid'];
$query = mysqli_query($db, "SELECT * FROM users JOIN users_in_roles ON users.id = users_in_roles.user_id WHERE users.id != $sessionid");
if(isset($_POST['delete'])){
$deleteid = $_GET['id'];
$deletequery = "DELETE FROM users WHERE id =?";
$deletestatement = $db->prepare($deletequery);
$deletestatement->bind_param('d', $deleteid);
$deletestatement->execute();
$deletestatement->store_result();
$deletesuccessful = $deletestatement->affected_rows == 1 ? true : false;
if($deletesuccessful){
$userid = $deletestatement->delete_id;
$deleteRoleQuery = "DELETE FROM users_in_roles WHERE id=?";
$deleterolestatement = $db->prepare($deleteRoleQuery);
$deleterolestatement->bind_param('d', $userid);
$deleterolestatement->execute();
$deleterolestatement->close();
header('location: editUsers.php');
} else{
echo "Failed";
}
}
if(isset($_REQUEST['update'])){
$updateid = $_REQUEST['id'];
$username = $_REQUEST['username'];
$userrole = $_REQUEST['roleid'];
$userpassword = $_REQUEST['password'];
$updatequery = "UPDATE users SET username=?, password=SHA(?) WHERE id =?";
$updatestatement = $db->prepare($updatequery);
$updatestatement->bind_param('ssd', $username, $password, $updateid);
$updatestatement->execute();
$updatestatement->store_result();
$updateSuccessful = $updatestatement->affected_rows == 1 ? true : false;
if($updateSuccessful){
$userid = $updatestatement->update_id;
$updateRoleQuery = "UPDATE users_in_roles SET role_id=? WHERE id=?";
$updaterolestatement = $db->prepare($updateRoleQuery);
$updaterolestatement->bind_param('dd', $userrole, $userid);
$updaterolestatement->execute();
$updaterolestatement->close();
header('location: editUsers.php');
}
else {
echo "Failed";
}
}
?>
<div id="container">
<?php
$row_count = mysqli_num_rows($query);
if ($row_count == 0) {
echo '<p style="color:red">No User details available</p>';
} elseif ($query) {
while($users = mysqli_fetch_array($query)){
$user_id = $users['id'];
$userrole = $users['role_id'];
$username = $users['username'];
echo '<div class="admin">';
echo '<form method="post" action="editUsers.php"';
echo '<input type="hidden" name="id" value="'.$user_id.'" />';
echo 'Username: <input type="text" name="username" value="'.$username.'" /><br>';
echo 'User Role: <input type="text" name="roleid" value="'.$userrole.'" /><br>(1 = admin, 2 = user)<br>';
echo 'Password: <input type="password" name="password" value="" /><br>';
echo '<input type="submit" name="update" value="Update" /> <input type="submit" name="delete" value="Delete" />';
echo '<br><br></form></div>';
}
}else {
die('There was a problem with the query: ' .$query->error);
}
mysqli_free_result($query);
?>
</div>
答案 0 :(得分:1)
在PHP中,未定义的索引通常是指一个哈希数组表达式,您试图获取哈希数组中当前不存在的键。
我看到两种情况,你试图得到一个可能导致这个错误的密钥'userid'(如果你注意到错误报告了哪个代码行会有帮助)。
$sessionid = $_SESSION['userid'];
您的会话数据中可能没有“用户ID”键。在尝试阅读之前,您应该检查是否已设置该密钥。
if(isset($_POST['delete'])){
$deleteid = $_GET['userid'];
这是另一种可能未在$ _GET超全局中设置'userid'键的情况。您应该在阅读之前检查它是否已设置。
答案 1 :(得分:0)
我认为你的错误引用的'userid'索引是$ _SESSION超全局中的索引,并且未设置的原因是你没有调用session_start()来启动/恢复会话。