What's new

Help Warning: Undefined array key "bpmsaid"

Status
Not open for further replies.

Alieson09

Forum Expert
Joined
Aug 30, 2021
Posts
2,246
Solutions
27
Reaction
2,961
Points
2,085
Warning: Undefined array key "bpmsaid"
PHP:
<?php
session_start();
include('includes/header.php');
include('includes/navbar.php');
include('includes/connect.php');

if (strlen($_SESSION['bpmsaid']==0)) {
  header('location:includes/navbar.php');
  } else{
if(isset($_POST['submit']))
{
$adminid=$_SESSION['bpmsaid'];
$cpassword=md5($_POST['currentpassword']);
$newpassword=md5($_POST['newpassword']);
$query=mysqli_query($con,"select ID from login where ID='$adminid' and   password='$cpassword'");
$row=mysqli_fetch_array($query);
if($row>0){
$ret=mysqli_query($con,"update login set password='$newpassword' where ID='$adminid'");
echo '<script>alert("Your password successully changed.")</script>';
} else {

echo '<script>alert("Your current password is wrong.")</script>';
}
}
?>
<script type="text/javascript">
function checkpass()
{
if(document.changepassword.newpassword.value!=document.changepassword.confirmpassword.value)
{
alert('New Password and Confirm Password field does not match');
document.changepassword.confirmpassword.focus();
return false;
}
return true;
}
</script>
<div class="content">
            <div class="animated fadeIn">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="card">                         
                        </div> <!-- .card -->
                    </div><!--/.col-->
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <strong>Change </strong> Password
                            </div>
                            <div class="card-body card-block">
                                <form action="" method="post" enctype="multipart/form-data" class="form-horizontal" name="changepassword" onsubmit="return checkpass();">
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Current Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="currentpassword" class=" form-control" required= "true" value=""></div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="email-input" class=" form-control-label">New Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="newpassword" class="form-control" value="" required="true"></div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="password-input" class=" form-control-label">Confirm Password</label></div>
                                        <div class="col-12 col-md-9"> <input type="password" name="confirmpassword" class="form-control" value="" required="true"></div>
                                    </div>                   
                                   <p style="text-align: center;"> <button type="submit" class="btn btn-primary btn-sm" name="submit" >Change</button></p>
                                </form>
                            </div>                           
                        </div>                       
                    </div>
                    <div class="col-lg-6">
                </div>
            </div>
        </div><!-- .animated -->
    </div><!-- .content -->

    <?php
include('includes/scripts.php');
include('includes/footer.php');
?>
<?php }  ?>
 
The notice "Undefined array key 'bpmsaid'" appears because the code is trying to access the 'bpmsaid' key of the $_SESSION superglobal variable, but this key has not been set or is not defined.

To fix this issue, you need to ensure that the 'bpmsaid' key is set in the $_SESSION array before the code tries to access it.

One way to do it is by adding a check to see whether the 'bpmsaid' key is set or not, before trying to check its length. Here's an updated version of the code with the check added:

Code:
<?php
session_start();
include('includes/header.php');
include('includes/navbar.php');
include('includes/connect.php');

if (!isset($_SESSION['bpmsaid']) || strlen($_SESSION['bpmsaid']) == 0) {
  header('location:includes/navbar.php');
} else {
  if(isset($_POST['submit'])) {
    $adminid=$_SESSION['bpmsaid'];
    $cpassword=md5($_POST['currentpassword']);
    $newpassword=md5($_POST['newpassword']);
    $query=mysqli_query($con,"select ID from login where ID='$adminid' and password='$cpassword'");
    $row=mysqli_fetch_array($query);
    if($row>0){
      $ret=mysqli_query($con,"update login set password='$newpassword' where ID='$adminid'");
      echo '<script>alert("Your password successully changed.")</script>';
    } else {
      echo '<script>alert("Your current password is wrong.")</script>';
    }
  }
}
?>

In the updated code, we added a check to see whether the 'bpmsaid' key is set, using the isset() function. If it's not set, the code redirects the user to the login page.

Also, we changed the condition in the first if statement from strlen($_SESSION['bpmsaid']==0) to strlen($_SESSION['bpmsaid']) == 0. This ensures that the code checks the length of the 'bpmsaid' value, rather than the length of the result of the comparison between the 'bpmsaid' value and zero, which would always be false.
 
Status
Not open for further replies.

Similar threads

Back
Top