drnf593779 2017-04-23 03:56
浏览 89

注意:未定义的索引:userid in ... PHP不发送隐藏的输入id

I have searched for a solution to my problem but have so far failed to come across a working solution.

I am trying to update a MySQL table using PHP but I keep getting the Error Undefined index: userid.

Below is all the code for my editUsers.php page. Can anyone see where I am going wrong?

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" />&nbsp;<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>
  • 写回答

2条回答 默认 最新

  • dongyang9813 2017-04-23 04:03
    关注

    In PHP, undefined index usually refers to a hash array expression where you tried to get a key that doesn't currently exist in the hash array.

    I see two cases where you are trying to get a key 'userid' that might be causing this error (it would help if you noted which code line is reported with the error).

    $sessionid = $_SESSION['userid'];
    

    You might not have a 'userid' key in your session data. You should check if that key is set before you try to read it.

    if(isset($_POST['delete'])){
    $deleteid = $_GET['userid'];
    

    This is another case where the 'userid' key might not be set in your $_GET superglobal. You should check if it is set before reading it.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么