I am unable to authenticate user

Asked

Viewed 157 times

0

I am not being able to do user authentication, just redirecting to the part where "it is not possible to register user".

<?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>";
        }
      }
    }
?>
  • 1

    At last else, place echo mysqli_error($connect) to display the latest error message in Mysqli.

  • curiosity, can reverse the order within parentheses $select = mysqli_query($query_select,$connect); would not be $select = mysqli_query($connect,$query_select);

  • Welcome to this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

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');

were replaced by a line

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

So much $select = mysqli_query($query_select,$connect); how much $insert = mysqli_query($query,$connect); reversed order within parentheses.

Tested, works and follows the full 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>";
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.