❓ Help Warning: Trying to access array offset on value of type null

Status
Not open for further replies.

Alieson09

Forum Guru
Warning: Trying to access array offset on value of type null
PHP:
<?php
$host="localhost";
$user="root";
$password="";
$db="bpms2db";

session_start();


$data=mysqli_connect($host,$user,$password,$db);

if($data===false)
{
    die("connection error");
}


if($_SERVER["REQUEST_METHOD"]=="POST")
{
    $email=$_POST["email"];
    $password=$_POST["password"];


    $sql="select * from login where Email ='".$email."' AND password='".$password."' ";

    $result=mysqli_query($data,$sql);

    $row=mysqli_fetch_array($result);
    $data->close();

    if($row["usertype"]=="user")
    {   

        $_SESSION["email"]=$email;

        header("location:dashboarduser.php");

    }

    elseif($row["usertype"]=="admin")
    {

        $_SESSION["email"]=$email;
        
        header("location:admin/index.php");
    }

    else
    {
        echo "<script>alert('email or password incorrect');</script>";

    }

}
 
The error "Trying to access array offset on value of type null" occurs when you try to access an array index that does not exist or has a null value. In your code, the error may occur when you try to access the $row array after executing the SQL query.

To fix this error, you can add a check to ensure that the $row array is not null before accessing its elements. You can also use mysqli_num_rows() function to check if any rows were returned from the SQL query.

Here's an updated version of your code that includes these checks:

Code:
<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "bpms2db";

session_start();

$data = mysqli_connect($host, $user, $password, $db);

if ($data === false) {
    die("connection error");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = $_POST["email"];
    $password = $_POST["password"];

    $sql = "select * from login where Email ='" . $email . "' AND password='" . $password . "' ";

    $result = mysqli_query($data, $sql);

    if (mysqli_num_rows($result) == 1) {
        $row = mysqli_fetch_array($result);
        $data->close();

        if ($row["usertype"] == "user") {
            $_SESSION["email"] = $email;
            header("location:dashboarduser.php");
        } elseif ($row["usertype"] == "admin") {
            $_SESSION["email"] = $email;
            header("location:admin/index.php");
        } else {
            echo "<script>alert('email or password incorrect');</script>";
        }
    } else {
        echo "<script>alert('email or password incorrect');</script>";
    }
}

In this updated code, we first check if the $result returned from the SQL query has any rows using mysqli_num_rows(). If it has exactly one row, we fetch the row using mysqli_fetch_array() and store it in the $row array. Then, we check if the $row array is not null before accessing its elements. If it's null or the email and password are incorrect, we display an error message.
 
Parse error: Unclosed '{' on line 9

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

if (strlen($_SESSION['bpmsaid']==0)) {
  header('location:logout.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>';
}
}
?>
<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');
?>
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 3K
    Views
  • 1
    Participants
Last reply from:
Alieson09

Trending Topics

Online now

Members online
887
Guests online
1,227
Total visitors
2,114

Forum statistics

Threads
2,274,073
Posts
28,953,480
Members
1,235,044
Latest member
hexthe1
Back
Top