Call to a member function bind_param ()

2

I am a beginner in PHP and am getting this error when trying to do an insert in the database using Mysqli functions:

  

Fatal error: Call to a member function bind_param() on string in C:\xampp\htdocs\Uc16\cadastro.php on line 5

What am I doing wrong?     

        $sql = "INSERT INTO tb_usuarios(user_nome,user_senha,user_nivel,user_cpf,user_end_rua,user_end_bairro,user_end_numero,user_tel,user_email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        $sql->bind_param('ssisssiss',
        $_POST['user_nome'], 
        $_POST['user_senha'],
        $_POST['user_nivel'],
        $_POST['user_cpf'],
        $_POST['user_end_rua'],
        $_POST['user_end_bairro'],
        $_POST['user_end_numero'],
        $_POST['user_tel'],
        $_POST['user_email']);
        if ($conn->query($sql) === TRUE) {
            echo "Registrado";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        $conn->close();

     ?>
    
asked by anonymous 24.10.2017 / 19:20

1 answer

2

Unable to bind in a string, it must be done in the connection variable. First of all you should call prepare()

$sql = "INSERT INTO tb_usuarios(user_nome,user_senha,user_nivel,user_cpf,user_end_rua,user_end_bairro,user_end_numero,user_tel,user_email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conexao->prepare($sql);
$stmt->bind_param(...);
if(!$stmt->execute()){
   echo 'erro: '. $conexao->error;
}
    
24.10.2017 / 19:23