Error inserting data in mysql and php

Asked

Viewed 39 times

0

I’m kind of a beginner dealing with php and would like some help to understand why this is happening

I am having the following problem, when I run the following php Function, it does not return anything to me in fetch as I asked and I cannot make the comparison correctly in if

I would like to find some solution to make the query, return the number of lines that exist in the query and make me can compare to know if the user exists or not to be able to register it in the database, Follow the code of Function

 function cadastrar($nome, $email, $senha, $empresa, $nmPage, $user){
     $stm="CALL insec(:user, :snha, :emal, :emp, :nmPega, :nme)";
     $pdo = new PDO('mysql:host=127.0.0.1;dbname=loginads', 'root', '');

     $sql = $pdo->prepare("SELECT COUNT(*) FROM loginuser where usuario = :user  or  email = :email");
     $sql -> bindValue(":email", $email);
     $sql -> bindValue(":user", $user);
     $sql -> execute();

    if (!$sql -> fetch() > 0) {
        echo"Login e senha ja existem";
        return false;
    }
    else {
        //cadastrar
        $sql = $pdo -> prepare ($stm);
        $sql -> bindValue(":usr", $user);
        $sql -> bindValue(":snha", $senha);
        $sql -> bindValue(":emal", $email);         
        $sql -> bindValue(":emp", $empresa);
        $sql -> bindValue(":nmPega", $nmPage);
        $sql -> bindValue(":nme", $nome);
        $sql -> execute();
        return true;
    }

}

this is the code of the connection with the bank:

 function conectar(){
    try {
        $pdo = new PDO('mysql:host=127.0.0.1;dbname=loginads', 'root', '');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return array('conexao' =>$pdo ,"mensagem" => "Sucesso" );
     
    } catch(PDOException $e) {
        return array("conexao" => null, "mensagem" =>'ERROR: <br> ' . $e->getMessage());
    }
}

and that’s the code that’s running Function :

require_once 'ProcessaDadosLogin.php';

$u = new Processa();

if(isset($_POST['nome']))
{
    $Nome = $_POST['nome'];
    $Senha = $_POST['senha'];
    $Empresa = $_POST['empresa'];
    $Email = $_POST['email'];
    $NmPage = $_POST['nmPage'];
    $Usuario = $_POST['user'];

    if (!empty($Nome) && !empty($Senha) && !empty($Empresa) && !empty($Email) && !empty($NmPage) && !empty($Usuario)) { 
        $u -> conectar();
        if (empty($msgError)) {
            if ($u -> cadastrar($Nome, $Email, $Senha, $Empresa, $NmPage, $Usuario)) {
                echo "cadComsucesso";
                header('Location: contatoht.php');
                                
            }
            else{
                echo "Login e senha ja existem";
            }
        }
        else{
            echo "erro de conexão" .$u -> $msgError;
        }
    }
    else{
        echo "preencha tudo, não deixe campos vazios";
    }
}
else{
        echo "preencha o campo nome";
}

in this part of the code I tried to use some things but not being able to identify where the error pfv need help, they were working before but I was not making any kind of comparison from database I am using the xammp, now does not register nor even shows any error message

  • He tried to clear the gaps between this symbol ->

1 answer

0


Hello,

It seems to me you’re not assigning the return of fetch for no one. On the lines:

if (!$sql -> fetch() > 0) {
    echo"Login e senha ja existem";
    return false;
}

Do the following:

$linhas = $sql->fetchAll();
if(count($linhas)){ 
    echo "Login e senha já existem";
    return false;
}

Browser other questions tagged

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