What's new

PHP 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 (!isset($_SESSION['bpmsaid']) || strlen($_SESSION['bpmsaid']) == 0) {
  header('location:change_password.php');
} else {
  if(isset($_POST['submit'])) {
    $adminid=$_SESSION['bpmsaid'];
    $cpassword=($_POST['currentpassword']);
    $newpassword=($_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();">
                                    <?php
$adminid=$_SESSION['bpmsaid'];
$ret=mysqli_query($con,"select * from login where ID='$adminid'");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
?>
                                    <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>
                                    <?php } ?>                   
                                   <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');
?>
 
The "Undefined array key 'bpmsaid'" warning occurs when the 'bpmsaid' index of the $_SESSION array is used without being previously defined or set. In this case, the code checks if the $_SESSION['bpmsaid'] variable isset() and has a length greater than 0. If it is not set or has a length of 0, the user is redirected to the change_password.php page.

To fix this warning, you need to make sure that the 'bpmsaid' index of the $_SESSION array is set before accessing it. One way to do that is to add a check like this at the beginning of the script:

Code:
if (!isset($_SESSION['bpmsaid'])) {
  $_SESSION['bpmsaid'] = '';
}

This will initialize the 'bpmsaid' index of the $_SESSION array with an empty string if it is not already set.
 
Status
Not open for further replies.

Similar threads

Back
Top