Only register once and then stop

Asked

Viewed 66 times

0

I am making a virtual sales store using HTML/CSS/PHP for the course work and am getting the following BUG:

I created the code for the processing of user registration, when I create the first user works and creates the data in the database. Everything works beautiful, but when I create another user does not work, does not create.

<?php 
// Vai incluir/importar a função/método que está no Conexao.php
    include 'Includes\Conexao.php';

// Receber o login, senha, nome, cpf, endereco e telefone
    $login = $_POST["login"];
    $senha = $_POST["senha"];
    $nome = $_POST["nome"];
    $cpf = $_POST["cpf"];
    $endereco = $_POST["endereco"];
    $telefone = $_POST["telefone"];

// Consulta se o login já é existente no banco de dados
    $consulta = mysql_query ("SELECT loginUser FROM usuarios WHERE loginUser = '$login'");
    $linhas = mysql_num_rows ($consulta); // Coloca na variável um valor com a quantidade de linhas encontradas na consulta feita anteriormente

// Se o resultado for verdadeiro, se existir o login no banco de dados, ele retornará uma mensagem e volta para a página Cadastra_User.php
    if ($linhas == 1) // ou true - Se o login existir
    {
        echo "<script> alert ('Login ja cadastrado com algum usuario. Tente novamente!') </script>";
        echo "<script> location.href = ('Cadastra_User.php') </script>";
        exit(); // se for verdadeiro, o fluxo para aqui!
    }
    else // Se o login não existir
    {
        $cadastrar = mysql_query("INSERT INTO usuarios (loginUser, senhaUser, nomeUser, cpfUser, endereco, telefone) VALUES ('$login', '$senha', '$nome', '$cpf', '$endereco', '$telefone')");

        if ($cadastrar == true)
        {
            echo "<script> alert ('$nomeUser cadastrado com sucesso!') </script>";
            echo "<script> location.href = ('Cadastra_User.php') </script>";
            exit(); // se for verdadeiro, o fluxo para e retorna para a página de cadastra_user
        }
        else
        {
            echo "<script> alert ('Ocorreu um erro no servidor. Tente novamente!') </script>";
        }   
    }       
?>

What appears when I try to create a second user:

inserir a descrição da imagem aqui

  • 1

    See how sql was mounted on the second call, use mysql_error to show which error occurred

  • you have an auto increment? field before writing the error message do, echo mysql_error();

1 answer

0


Generally, this type of error of not being able to register more than one record in a table is occasioned when you define a Primary key (for example, in the field id) and forgets to put it as AUTO_INCREMENT.

What will happen if you don’t define it as AUTO_INCREMENT is an insertion of a value 0 to the country id, since you defined it as INTEGER. When you don’t have AUTO_INCREMENT, the default value of a column INTEGER uninformed is always 0.

I mean, you’re hoping that the MYSQL increment for you, but it was not set to increment. As you defined id as Primary key, when entering the record, 0 becomes as Primary Key. If you try to enter another record, it will try to insert as 0 again, but this will generate an error due Primary Keys cannot duplicate each other.

Browser other questions tagged

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