I can not authenticate user

0

I am not able to do user authentication, it is only redirecting to the part where "user registration is not possible".

<?php 

$login = $_POST['login'];
$senha = MD5($_POST['senha']);
$connect = mysqli_connect('nome_do_servidor','nome_de_usuario','senha');
$db = mysqli_select_db('nome_do_banco_de_dados');
$query_select = "SELECT login FROM usuarios WHERE login = '$login'";
$select = mysqli_query($query_select,$connect);
$array = mysqli_fetch_array($select);
$logarray = $array['login'];

  if($login == "" || $login == null){
    echo"<script language='javascript' type='text/javascript'>alert('O campo login deve ser preenchido');window.location.href='cadastro.html';</script>";

    }else{
      if($logarray == $login){

        echo"<script language='javascript' type='text/javascript'>alert('Esse login já existe');window.location.href='cadastro.html';</script>";
        die();

      }else{
        $query = "INSERT INTO usuarios (login,senha) VALUES ('$login','$senha')";
        $insert = mysqli_query($query,$connect);

        if($insert){
          echo"<script language='javascript' type='text/javascript'>alert('Usuário cadastrado com sucesso!');window.location.href='login.html'</script>";
        }else{
          echo"<script language='javascript' type='text/javascript'>alert('Não foi possível cadastrar esse usuário');window.location.href='cadastro.html'</script>";
        }
      }
    }
?>
    
asked by anonymous 27.09.2017 / 03:25

1 answer

0

Corrections made:

these two lines

$connect = mysqli_connect('nome_do_servidor','nome_de_usuario','senha');
$db = mysqli_select_db('nome_do_banco_de_dados');

have been replaced by a line

$connect = mysqli_connect('nome_do_servidor','nome_de_usuario','senha','nome_do_banco');

Both $select = mysqli_query($query_select,$connect); and $insert = mysqli_query($query,$connect); inverted the order within the parentheses.

I tested it, it works and it follows the complete code.

$connect = mysqli_connect('nome_do_servidor','nome_de_usuario','senha','nome_do_banco');

$query_select = "SELECT login FROM usuarios WHERE login = '$login'";
$select = mysqli_query($connect,$query_select);
$array = mysqli_fetch_array($select);
$logarray = $array['login'];

if($login == "" || $login == null){
    echo"<script language='javascript' type='text/javascript'>alert('O campo login deve ser preenchido');window.location.href='cadastro.html';</script>";
}else{
  if($logarray == $login){

    echo"<script language='javascript' type='text/javascript'>alert('Esse login já existe');window.location.href='cadastro.html';</script>";
    die();

  }else{
    $query = "INSERT INTO usuarios (login,senha) VALUES ('$login','$senha')";
    $insert = mysqli_query($connect,$query);

    if($insert){
      echo"<script language='javascript' type='text/javascript'>alert('Usuário cadastrado com sucesso!');window.location.href='login.html'</script>";
    }else{
      echo"<script language='javascript' type='text/javascript'>alert('Não foi possível cadastrar esse usuário');window.location.href='cadastro.html'</script>";
    }
  }
}
    
27.09.2017 / 04:10