I'm trying to make a simple login. The idea is to look in the database for the user and password.
The fact is that I have a SELECT
that should take the rows with name and user that match those previously entered by the user.
The SELECT
does not take any row, that is, it is not the fault of SELECT
but of the statement to connect to the database.
Code validar.php
, where the failure occurs:
<?php
include 'serv.php';
if(isset($_POST['login'])){
$usuario = $_POST['user'];
$pw = $_POST['pw'];
$log = mysqli_query("SELECT * FROM noticiasBD WHERE user='$usuario' AND pw='$pw'");
echo "tenemos: $log";
echo '<script> alert("tenemos: $log");</script>';
if (mysqli_num_rows($log)>0) {
$row = mysql_fetch_array($log);
$_SESSION["user"] = $row['user'];
echo 'Iniciando sesión para '.$_SESSION['user'].' <p>';
echo '<script> window.location="panel.php"; </script>';
}
else{
echo '<script> alert("Usuario o contraseña incorrectos.");</script>';
echo '<script> window.location="index.php"; </script>';
}
}
?>
Here I have the form where I call validad.php
. It's called index.php
:
<?php
session_start();
include 'serv.php';
if(isset($_SESSION['user'])){
echo '<script> window.location="panel.php"; </script>';
}
?>
<h1 class="h1" style="color:white">Login</h1>
<form method="post" action="validar.php">
<input type="text" class="form-control" name="user" autocomplete="off" required><br><br>
<input type="password" class="form-control" name="pw" autocomplete="off" required><br><br>
<input type="submit" class="btn btn-success" name="login" value="Entrar">
</form>
And I connect to the database from serv.php
, which does not fail:
<?php
$conect = mysqli_connect('localhost', '--', '--', 'c1jormacolBD')
or die('Error: ' . mysqli_connect_error());
Image from phpMyAdmin:
It connects well to the database but not to the table in question.