Error in php/mysql register

Asked

Viewed 159 times

1

I made a login and registration system, and within the registration section I put a condition, if true register, if false do not register. However, as much as if Else is true, when I enter the database, the registration was not carried out. How do I solve this problem?

require_once ('connect.php');
    
    //recebendo os dados
    $user =  $_POST['user'];
    $email =  $_POST['email'];
    $senha =  md5($_POST['senha']);
    $rsenha = $_POST['rsenha'];
    $birth = $_POST['birth'];
if($connect==true){
        //inserindo os dados
    $cadastro = mysqli_query($connect,"INSERT INTO Usuarios (user, email, senha, birth, cadastro) VALUES ('$user', '$email', '$senha', '$birth', NOW())");
        echo "<script>
                alert('Cadastro efetuado com sucesso, basta logar!!');
                window.location.href='../index.php';
            </script>";
    }else{
        echo "<script>
                alert('Não foi possível se cadastrar, tente novamente mais tarde');
                window.location.href='../Cadastro.php';
            </script>";
    }

That’s the code, thank you all for helping :)

  • Whenever an answer solves your difficulty mark it as accepted, see https://i.stack.Imgur.com/evLUR.png and what it means in https://answall.com/help/accepted-answer

  • Logical error, your condition will issue an alert if your connection works, regardless of saving the data or not.

3 answers

1

mysqli_connect() always returns a Mysqli object. To check for connection errors, use:

$connect = new MySQLi('localhost', 'usuario', 'senha', 'db');

if ($connect->connect_error) {
   echo "Não conectou, erro: " . $connect->connect_error;
}
else {

   /* ######## e caso não haja erros faça o seu INSERT aqui ######### */
    //inserindo os dados
    $cadastro = mysqli_query($connect,"INSERT INTO Usuarios ....
    ....................
    .............

}

If the goal is to verify if the Insert was realized then make use of mysqli_affected_rows - returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query associated with the given link parameter

1

You can validate if it was registered and if the check is false collect the mysql error.

$cadastro = mysqli_query($connect,"INSERT INTO Usuarios (user, email, senha, birth, cadastro) VALUES ('$user', '$email', '$senha', '$birth', NOW())");
if (!$cadastro) {
   print_r(mysql_error());
}

My guess would be because of the date, maybe you’re sending it in Brazilian format, and mysql stores it in ISO format (YYYY-MM-DD HH:MM:SS).

  • How would I fix this NOW mistake??

  • Surely it’s not the now() the problem, must be with the variable $birth.

1

As I said in a comment, there is a logical error in your code.

<?php

require_once ('connect.php');

        //recebendo os dados
$user =  $_POST['user'];
$email =  $_POST['email'];
$senha =  md5($_POST['senha']);
$rsenha = $_POST['rsenha'];
$birth = $_POST['birth'];
            //inserindo os dados
$cadastro = mysqli_query($connect,"INSERT INTO Usuarios (user, email, senha, birth, cadastro) VALUES ('$user', '$email', '$senha', '$birth', NOW())");
if($cadastro){
    echo "<script>
    alert('Cadastro efetuado com sucesso, basta logar!!');
    window.location.href='../index.php';
    </script>";
}else{
    echo "<script>
    alert('Não foi possível se cadastrar, tente novamente mais tarde');
    window.location.href='../Cadastro.php';
    </script>";
}

Ps. did not test, but corrected the logical error, however test what is the return of $cadastro to correct the condition.

EDIT 1

Try to use this method $mysqli->error, within your Else to get the errors of your query, or even try to run this query directly in your Mysql Client. to see if there is no syntax error. Try this:

else{
    echo $mysqli_error($connect);
    die;
}
  • I made the correction, the message says it is not registering and it is not actually registering. The error of sending data is in the code?

  • With this 'mysqli error' I will discover the problem?

  • I believe so, unless there’s some other problem other than the consultation

Browser other questions tagged

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