Difficulty checking php data with mysql

Asked

Viewed 125 times

-2

I’m trying to check the bank to know if you already have a user with registered email typed by hmtl, I did this php script, but the execution always stops at the first if even if the email typed is not in the bank yet.

I believe that the logic I used is correct, I can’t identify where I’m going wrong

<?php
include_once("conexao_class.php");
include_once("usuario_class.php");


$My = new MySQLiConnection();


$nome = $_POST['nome'];
$sobrenome = $_POST['sobrenome'];
$email = $_POST['email'];
$sexo = $_POST['sexo'];
$telefone_fixo = $_POST['telefone'];
$telefone_movel = $_POST['celular'];
$senha = $_POST['senha'];

$obj_usu = new usuario($nome,$sobrenome,$telefone_fixo,$telefone_movel,$email,$sexo,$senha);



$My = new MySQLiConnection();// conecta-se automaticamente ao servidor MySQL
  $verifica = "SELECT * FROM tb_usuario WHERE nm_email = '$email'";

// a conexão é fechada automaticamente no fim do script.
      // retornando a falta de paramentro ao ajax      

      $result = $My->query($verifica) or die(mysql_error());



        if (mysqli_num_rows($result)<=0)
      {
            if(isset($_POST['terms']))
                {

                $result2 = $obj_usu->AddUsuario();
                echo"$result2";
                }
                else{
                // retornando ao ajax dados inválidos
                      echo"3";
                    }
      }


    else{
    // retornando ao ajax email já cadastrado
         echo"1";
         }
?>
  • The logic is correct, who knows the error is in another part of the code not posted. Probably in the email input

  • i checked input and is correct, test also return the email variable to see if it was filled and actually returned the email, I have no idea where is the error

  • You just can’t go back echo"3";

  • When an answer solves your problem, do not answer in the answer box and do not need to thank in comments, just mark the answer as accepted. See how in https://i.stack.Imgur.com/evLUR.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

1 answer

0

That condition if(isset($_POST['terms']) always returns true when submitting the form even if the input has not been completed

Besides testing if posts have been started check that they are not empty

 // verifique se foi iniciado e não está vazio
 if (isset($_POST["email"]) && !empty($_POST["email"])) {

 ...............
 ..............

    if (mysqli_num_rows($result)<=0){

        // verifique se foi iniciado e não está vazio
        if (isset($_POST["terms"]) && !empty($_POST["terms"]))
        {

            echo "2";
        }
        else{
            // retornando ao ajax dados inválidos
                  echo"3";
        }
    }
    else{
     // retornando ao ajax email já cadastrado
     echo"1";
    }

 .....................
 .....................
  • Friend, I made the change you passed and it didn’t work, it turns out I used this code and it worked perfectly, today I went to make some changes because I’m using POO now and gave this problem, the script is practically the same

  • @Reignomo see if this should work in this example http://kithomepage.com/sos/verificacao-dados.php

  • @Reignomo, with the code you posted, you can only see wrong what I said in the answer. Otherwise, only God or a soothsayer can imagine the rest of your code to analyze.

Browser other questions tagged

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