Successful php registration response

Asked

Viewed 57 times

-1

I’m trying to make a return "Registration made successfully" but always falls in Else, he is not counting the number of lines.

public function adicionarAluno(string $nome, string $matricula, string $endereco, string $tel): void{

        $adicionar=$this->mysql->prepare("INSERT INTO aluno (nome,matricula,endereco,tel) VALUES (?,?,?,?);");
        $adicionar->bind_param('ssss',$nome,$matricula,$endereco,$tel);
        $adicionar->execute();
        $teste=$adicionar->num_rows;
        if($teste > 0){
            echo "<script>alert('Cadastro realizado com sucesso!');</script>";
        }else{
            echo "<script>alert('Falha!');</script>";
        }


    }
  • replaces $add->num_rows for $add->affected_rows

  • Thank you very much, now it’s working!!

1 answer

0

The registration is being carried out? Or it goes straight to LSE without registering?

Have you adapted this function from any other language? It’s just that I don’t remember having to use variable type declaration (string) in php, and I never did any function that used "void".

Try to leave it like this and see if it works, if it doesn’t work it returns here and leave the code of your connection file:

public function adicionarAluno($nome, $matricula, $endereco, $tel){

    $adicionar=$this->mysql->prepare("INSERT INTO aluno (nome,matricula,endereco,tel) VALUES (?,?,?,?);");
    $adicionar->bind_param('ssss',$nome,$matricula,$endereco,$tel);
    $adicionar->execute();
    $teste=$adicionar->num_rows;
    if($teste > 0){
        echo "<script>alert('Cadastro realizado com sucesso!');</script>";
    }else{
        echo "<script>alert('Falha!');</script>";
    }


}
  • Type statements within the functions are relatively old PHP 5. something. Now returns type statements are from PHP 7. Check out: https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

  • Ops.. I selected the link to return type declarations https://www.php.net/manual/en/functions.returning-values.php

  • Opa, thanks for the links, will be a great help. It’s been a long time since I dealt with PHP, and I know almost nothing about 7, will help me a lot =)

Browser other questions tagged

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