I am trying to login with php and msql and when entering an unregistered user it shows me this
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\Store\admin_control\login\validar.php on line 18
Warning: Trying to access array offset on value of type null in C:\xa[![enter image description here][1]][1]mpp\htdocs\Shop\admin_control\login\validar.php on line 22
<?php
$usuario=$_POST['usuario'];
$clave=$_POST['clave'];
session_start();
$_SESSION['usuario']=$usuario;
include('db.php');
$consulta="SELECT*FROM usuarios WHERE usuario='$usuario' and clave='$clave'";
$resultado=mysqli_query($conexion,$consulta);
$filas=mysqli_fetch_array($resultado);
if($filas['id_cargo'] == 1) { //ADMINSTRADOR
header("location:../../admin/productos.php");
}else if($filas['id_cargo'] == 2){ // CLIENTE
header("location:../../index.php");
}
else{
?>
<?php
include("index.php");
?>
<h1 class="bad">No existe ningun usuario con esas creedenciales <a href="#">¿Deseas crear una cuenta?</a> </h1>
<?php
}
mysqli_free_result($resultado);
mysqli_close($conexion);
The error is because $rows=mysqli_fetch_array($result); it may not have anything because you don't validate whether or not there are results coming from the database.
You should do this before:
With this you validate if the query to the database has or not results, that is to say if the login is correct or not, I also recommend you to change a bit to mysqli prepared queries to avoid injections to the database, then maybe migrate to some framework like Laravel, however with what I just gave you as an example will suffice.