Inserting data with PHP using classes and PDO

Asked

Viewed 341 times

0

Good evening guys. I have the task of creating a complete CRUD on the site where I work. However I need to use classes and methods with PHP. See what happens.

I have the class usuario.class, where I put all attributes and methods Get and Set, as well as data insertion methods.

public function insert(){

    try{
        $insert = $this->con->conectar()->prepare("INSERT INTO web_cadcli(nm_cliente, email_cliente, senha_cliente, id_identpess, img) values(:nome, :email, :Senha, :value,  :imagem);");
        $insert->bindParam(":nome",  $this->nome, PDO::PARAM_STR);
        $insert->bindParam(":email", $this->email, PDO::PARAM_STR);
        $insert->bindParam("senha", $this->senha, PDO::PARAM_STR);
        $insert->bindPAram(":value", $this->tpessoa, PDO::PARAM_STR);
        $insert->bindParam(":imagem",$this->imagem, PDO::PARAM_STR);

        if($insert->execute()){
            return 'ok';
        }else{
            return 'erro';
        }  
    }catch(PDOexception $erro_1){
        echo 'erro'.$erro_1->getMessage();
    }
}

It happens that when accessing the template gives error in the insertion line where the execute. I step the values through the code below contained in the form.

$usuario = new Usuario();

// Cadastro de Usuario
if ( isset($_POST['cadastrar']) ):

    $nome  = $_POST['nome'];
    $email = $_POST['email'];
    $senha = $_POST['senha'];
    $imagem= $_POST['imagem'];

    $usuario->setNome($nome);
    $usuario->setEmail($email);
    $usuario->setSenha($senha);
    $usuario->setImagem($imagem);

    if($usuario->insert()){

        echo '<div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>OK!</strong> Incluido com sucesso!!! </div>';

    } else {
        echo '<div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>OK!</strong> Erro ao Cadastrar!!! </div>';
    }
endif;
  • 1

    Welcome to the community, Rodrigo. First I invite you to do the [tour] to learn the basics of the site. [en.so] has native support for codes, so avoid posting them in image form. Just you [Edit] the question, paste your code, select it and press the shortcut Ctrl+K to format it correctly. In case you want to do manual, just add 4 blank spaces before each line of code.

  • 1

    In advance, at the end of line 64 there seems to be a ; that doesn’t make much sense.

  • Rodrigo, please edit your question by swapping the images for your code in formatted text as indicated in the comment above. Also at the end of your leg it seems that lacked an image.

  • I even imagine that, at the end of this question, the best thing would be to create another question to deal exclusively with this topic. However, be careful not to generate a question that tends to opinion-based answers.

1 answer

0


From the image you posted, your problem is on lines 64 and 67. On line 64 you set the parameter :Senha (with uppercase "S"). In line 67 you use the parameter :senha (with the tiny "s").

That is, the parameter :senha with lowercase "s" does not exist. Edit line 64 and change the parameter :Senha for :senha and it will work.

  • 1

    Success, when making the corrections suggested by Bruno Peres, the Insert worked perfectly...thanks

  • @Rodrigozanetti You’re welcome! Good that it worked. Please, if this answer has solved your problem remember accept it to inform other users that your issue has been resolved and help future viewers of your issue. Thank you!

Browser other questions tagged

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