Problem with bindParam - PHP

Asked

Viewed 510 times

3

I’m having problems with bindParam, someone could help me?

I have a user registration code in the bank...

<?php

require_once 'init.php';

// Pega os dados do formulario
$nome = isset($_POST['nome']) ? $_POST['nome'] : null;
$nascimento = isset($_POST['nascimento']) ? $_POST['nascimento'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$senha = isset($_POST['senha']) ? $_POST['senha'] : null;
$seg_senha = sha1($senha);  // Senha criptografada

// Validação bem simples, só para evitar dados vazios
if(empty($nome) || empty($nascimento) || empty($email) || empty($senha)){
    echo 'Volte e preencha todos os campos';
    exit;
}

// Insere no banco de dados
$pdo = db_connect();
$sql = "INSERT INTO usuarios(nome, nasc, email, senha)
        values(:nome, :nasc, :email, :senha)";
$stmt = $pdo->prepare($sql);
$stmt = bindParam(':nome', $nome);
$stmt = bindParam(':nasc', $nascimento);
$stmt = bindParam(':email', $email);
$stmt = bindParam(':senha', $seg_senha);

if($stmt->execute()){
    header('Location: index1.php');
}else{
    echo 'Erro ao cadastrar usuario no banco';
    print_r($stmt->errorInfo());
}

When I run it returns the following error: Fatal error: Call to Undefined Function bindParam() in C: wamp64 www Projectlpha core cadastrar.php on line 23

It accuses error right in the first bindParam... $stmt = bindParam(':name', $name);

I don’t know how to fix.

1 answer

7


There is an error in the way you implemented BIND.

Where has $stmt = bindParam should be $stmt->bindParam

See example below:

<?php

require_once 'init.php';

// Pega os dados do formulario
$nome = isset($_POST['nome']) ? $_POST['nome'] : null;
$nascimento = isset($_POST['nascimento']) ? $_POST['nascimento'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$senha = isset($_POST['senha']) ? $_POST['senha'] : null;
$seg_senha = sha1($senha);  // Senha criptografada

// Validação bem simples, só para evitar dados vazios
if(empty($nome) || empty($nascimento) || empty($email) || empty($senha)){
    echo 'Volte e preencha todos os campos';
    exit;
}

// Insere no banco de dados
$pdo = db_connect();
$sql = "INSERT INTO usuarios(nome, nasc, email, senha)
        values(:nome, :nasc, :email, :senha)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':nome', $nome);
$stmt->bindParam(':nasc', $nascimento);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':senha', $seg_senha);

if($stmt->execute()){
    header('Location: index1.php');
}else{
    echo 'Erro ao cadastrar usuario no banco';
    print_r($stmt->errorInfo());
}

I hope I’ve helped!

  • Putz -_-' That’s right guy, I hadn’t even noticed. Thank you!

  • Glad you solved it. Don’t forget to mark my answer as chosen!

Browser other questions tagged

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