What's new

Help Login help

Status
Not open for further replies.

Alieson09

Forum Expert
Joined
Aug 30, 2021
Posts
2,246
Solutions
27
Reaction
2,961
Points
2,085
Trying to access array offset on value of type null in login.php on line 12 and 15
PHP:
<?php
session_start();

if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $conn = mysqli_connect('localhost', 'root', '', 'user_login');
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_array($result); {
        if ($row["user_type"] == "user") {
            $_SESSION["username"] = $username;
            header("location:indexuser.php");
        } elseif ($row["user_type"] == "admin") {
            $_SESSION["username"] = $username;
            header("location:index.php");
        } else {
            echo "<script>alert('email or password incorrect');</script>";
        }
    }
}
?>

<form method="POST" action="">
    <label>Username:</label>
    <input type="text" name="username" required><br>

    <label>Password:</label>
    <input type="password" name="password" required><br>

    <input type="submit" name="submit" value="Login">
</form>
 
The error message "Trying to access array offset on value of type null" in your code indicates that you are trying to access an array index that does not exist, i.e., the value that is supposed to be at that index is null.

The error is most likely caused by the $row variable being null. This can happen if the query returned no results or if there was an error in the query. You can use the mysqli_error() function to check for errors in your query.

To fix the error, you can check if $row is not null before accessing its array indexes. For example, you can add an if statement before the conditional statements to check if $row is not null:

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

This way, if $row is null, the code inside the else statement will execute and display an error message.

Additionally, it is recommended to use prepared statements to prevent SQL injection attacks. You can read more about prepared statements in the PHP documentation.
 
The error message "Trying to access array offset on value of type null" in your code indicates that you are trying to access an array index that does not exist, i.e., the value that is supposed to be at that index is null.

The error is most likely caused by the $row variable being null. This can happen if the query returned no results or if there was an error in the query. You can use the mysqli_error() function to check for errors in your query.

To fix the error, you can check if $row is not null before accessing its array indexes. For example, you can add an if statement before the conditional statements to check if $row is not null:

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

This way, if $row is null, the code inside the else statement will execute and display an error message.

Additionally, it is recommended to use prepared statements to prevent SQL injection attacks. You can read more about prepared statements in the PHP documentation.
create a code to show reg users
 
create a code to show reg users
To show registered users in your database, you can use the following PHP code:

Code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'user_login');

// Retrieve all users from the database
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

// Display the users in a table
echo "<table>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th></tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['username'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "</tr>";
}
echo "</table>";

// Close the database connection
mysqli_close($conn);
?>

This code connects to your database using mysqli_connect and retrieves all users from the "users" table using a SELECT statement. It then displays the users in an HTML table with columns for ID, username, and email. Finally, it closes the database connection using mysqli_close.

You can customize this code to fit your specific needs, such as adding more columns to the table or applying filters to the SELECT statement.
 
To show registered users in your database, you can use the following PHP code:

Code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'user_login');

// Retrieve all users from the database
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

// Display the users in a table
echo "<table>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th></tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['username'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "</tr>";
}
echo "</table>";

// Close the database connection
mysqli_close($conn);
?>

This code connects to your database using mysqli_connect and retrieves all users from the "users" table using a SELECT statement. It then displays the users in an HTML table with columns for ID, username, and email. Finally, it closes the database connection using mysqli_close.

You can customize this code to fit your specific needs, such as adding more columns to the table or applying filters to the SELECT statement.
with add and delete
 
with add and delete
To add and delete users from your database, you can modify the code I provided earlier as follows:

Code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'user_login');

// Add a new user to the database
if (isset($_POST['add_user'])) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
    mysqli_query($conn, $query);
}

// Delete a user from the database
if (isset($_GET['delete_user'])) {
    $id = $_GET['delete_user'];
    $query = "DELETE FROM users WHERE id=$id";
    mysqli_query($conn, $query);
}

// Retrieve all users from the database
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

// Display the users in a table
echo "<table>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th><th></th></tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['username'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td><a href='?delete_user=" . $row['id'] . "'>Delete</a></td>";
    echo "</tr>";
}
echo "</table>";

// Add a form to add a new user
echo "<h2>Add User</h2>";
echo "<form method='POST' action=''>";
echo "<label>Username: </label><input type='text' name='username' required><br>";
echo "<label>Email: </label><input type='email' name='email' required><br>";
echo "<label>Password: </label><input type='password' name='password' required><br>";
echo "<input type='submit' name='add_user' value='Add User'>";
echo "</form>";

// Close the database connection
mysqli_close($conn);
?>

This code adds functionality to add and delete users from the "users" table.

When the "Add User" form is submitted, the code retrieves the values from the form, constructs an INSERT statement, and executes it using mysqli_query.

When the "Delete" link is clicked, the code retrieves the ID of the user to be deleted from the URL, constructs a DELETE statement, and executes it using mysqli_query.

The rest of the code is the same as before, retrieving and displaying all users from the "users" table in an HTML table.

Note that this code only provides basic functionality and may not be suitable for production environments. You should consider adding additional security measures, such as input validation and authentication, to your code before deploying it to a production environment.
 
To add and delete users from your database, you can modify the code I provided earlier as follows:

Code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'user_login');

// Add a new user to the database
if (isset($_POST['add_user'])) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
    mysqli_query($conn, $query);
}

// Delete a user from the database
if (isset($_GET['delete_user'])) {
    $id = $_GET['delete_user'];
    $query = "DELETE FROM users WHERE id=$id";
    mysqli_query($conn, $query);
}

// Retrieve all users from the database
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

// Display the users in a table
echo "<table>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th><th></th></tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['username'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td><a href='?delete_user=" . $row['id'] . "'>Delete</a></td>";
    echo "</tr>";
}
echo "</table>";

// Add a form to add a new user
echo "<h2>Add User</h2>";
echo "<form method='POST' action=''>";
echo "<label>Username: </label><input type='text' name='username' required><br>";
echo "<label>Email: </label><input type='email' name='email' required><br>";
echo "<label>Password: </label><input type='password' name='password' required><br>";
echo "<input type='submit' name='add_user' value='Add User'>";
echo "</form>";

// Close the database connection
mysqli_close($conn);
?>

This code adds functionality to add and delete users from the "users" table.

When the "Add User" form is submitted, the code retrieves the values from the form, constructs an INSERT statement, and executes it using mysqli_query.

When the "Delete" link is clicked, the code retrieves the ID of the user to be deleted from the URL, constructs a DELETE statement, and executes it using mysqli_query.

The rest of the code is the same as before, retrieving and displaying all users from the "users" table in an HTML table.

Note that this code only provides basic functionality and may not be suitable for production environments. You should consider adding additional security measures, such as input validation and authentication, to your code before deploying it to a production environment.
Parse error: syntax error, unexpected token "," in line 10
PHP:
<?php
session_start();
error_reporting(0);
include('connect.php');


if (isset($_POST['login'])) {
    $email = $_POST["email"];
    $password = $_POST["password"];
    $query = ($con,"SELECT * FROM login WHERE Email='$email' AND Password='$password'");
    $result = mysqli_query($query);
    $row = mysqli_fetch_array($result); {
        if ($row) {
        if ($row["usertype"] == "user") {
            $_SESSION["Email"] = $email;
            header("location:user/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>";
}
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>BPMS - Login</title>

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.css" rel="stylesheet">
    
</head>

<body class="bg-gradient-primary">
    <header>
        <div class="logo">
        <p>Bicycle Parking Management System</p>
              </div>
      <nav>
        <ul>
          <li><a href="#" class="navf">Home</a></li>
          <li><a href="#" class="navf">About</a></li>
          <li><a href="#" class="navf">Services</a></li>
            </ul>
          </nav>
        </header>
    <div class="container">

        <!-- Outer Row -->
        <div class="row justify-content-center">

            <div class="col-xl-10 col-lg-12 col-md-9">

                <div class="card o-hidden border-0 shadow-lg my-5">
                    <div class="card-body p-0">
                        <!-- Nested Row within Card Body -->
                        <div class="row">
                            <div class="col-lg-6 d-none d-lg-block bg-login-image"></div>
                            <div class="col-lg-6">
                                <div class="p-5">
                                    <div class="text-center">
                                        <h1 class="h4 text-gray-900 mb-4">Welcome To BPMS !</h1>
                                    </div>
                                    <form class="user" method="POST">
                                        <div class="form-group">
                                            <input type="email" class="form-control form-control-user"
                                                id="exampleInputEmail" aria-describedby="emailHelp" name="email"
                                                placeholder="Email Address..." required="true">
                                        </div>
                                        <div class="form-group">
                                            <input type="password" class="form-control form-control-user"
                                                id="exampleInputPassword" placeholder="Password" name="password" required="true">
                                        </div>
                                        <div class="form-group">
                                            <div class="custom-control custom-checkbox small">
                                                <input type="checkbox" class="custom-control-input" id="customCheck">
                                                <label class="custom-control-label" for="customCheck">Remember
                                                    Me</label>
                                            </div>
                                        </div>
                                        <button type="submit" name="login" class="btn btn-primary btn-user btn-block">
                                            Login
                                        </button>
                                        <hr>
                                    </form>
                                    <hr>
                                    <div class="text-center">
                                        <a class="small" href="forgotpassword.php">Forgot Password?</a>
                                    </div>
                                    <div class="text-center">
                                        <a class="small" href="signup.php">Create an Account!</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

        </div>

    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>

</body>

</html>
 
Status
Not open for further replies.

Similar threads

Back
Top