My first registration and login system does not work, what is the error?

Asked

Viewed 247 times

-3

When I go to the page to register, it already displays the message "This login already exists" and does not even let me enter the data. I think the error is in PHP, but I don’t know where.

If anyone can help me solve it, I haven’t implemented any security systems either because I’m learning and I don’t know if there are other bugs in the code.

This is the code of the register:

<?php 

$login = $_POST['login'];
$senha = MD5($_POST['senha']);
$connect = mysql_connect('nome_do_servidor','nome_de_usuario','senha');
$db = mysql_select_db('nome_do_banco_de_dados');
$query_select = "SELECT login FROM usuarios WHERE login = '$login'";
$select = mysql_query($query_select,$connect);
$array = mysql_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 = mysql_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>";
    }
  }
}
?>
  • I believe that information is missing. The code you posted does not show where the return comes from "User already exists."

  • 1

    The function mysql_query() has already been removed from php just to warn.

  • The message is "'That login already exists" (tidied up now) in the first echo within the second if,

  • Enter the code of cadastro.html, after all that’s where the problem occurs

  • Just to reinforce what @rray said, the functions mysql_ are obsolete and have been removed in php 7, use mysqli_ or PDO

  • Welcome Guilherme Geek, don’t miss this image https://i.stack.Imgur.com/evLUR.png and read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Show 1 more comment

1 answer

1

As has already been said mysql are obsolete and have been removed from PHP 7.

Use mysqli or PDO, it will not hurt anything, see

<?php 

if(isset($_POST["login"])){
    $db = new mysqli("localhost", "USUARIO", "SENHA", "nome_DB");

    $login = $_POST["login"];
    $senha = $_POST["senha"];

    $result = $db->query("SELECT COUNT(*) FROM usuarios WHERE login = '$login'");

    $row = $result->fetch_row();


    if ($row[0] > 0) {

        //usuario existente, etc.....

    } else {


        $sql = 'INSERT INTO usuarios(login,senha) VALUES(?, ?)';

        $stmt = $db->prepare($sql);
        if(!$stmt){
            echo 'erro na consulta: '. $db->errno .' - '. $db->error;
        }

        $var1 = $login;
        $var2 = $senha;
        $stmt->bind_param('ss', $var1, $var2);
        $stmt->execute();

    }

    mysqli_close($db);
}

?>
  • localhost would be the server address? and what would be the name_db?

  • I’m sorry, I’m really new at this

  • @Guilhermegeek, do you have a database? name_db is the name of the database. If you have just put the USER, PASSWORD and the database name in $db = new mysqli("localhost", "USER", "PASSWORD", "DName"); localhost leaves as is. Google has the setting of localhost.

  • But my database is on another server, even so I leave localhost? because I put the address of the server. And the initial problem persisted, which is the message "This login already exists" before the page display.

  • I don’t know how this works (database on another server). I never made use of this situation. In question this was not specified. Ask another question reporting this.

  • Sorry, I’m talking nonsense. I left localhost and still error, the message before the form.

Show 1 more comment

Browser other questions tagged

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