Insert function is not working

1

I have the following class in php:

<?php
class contaEntrada {

public $mostraDados;
public $insereDados;

function conectar(){
    $host = "localhost";
    $user = "root";
    $pass = "root";
    $dbname = "fluxo_de_caixa";

    try {
        $opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $pdo = new PDO("mysql:host=localhost;dbname=fluxo_de_caixa;", "root", "root", $opcoes);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    return $pdo;
}

function insereConta($id_empresa, $cat, $subcat, $val, $forPag, $data){
    $pdo = conectar();
    $val = floatval(str_replace(',', '.', str_replace('.', '', $val)));

    if($data == ''){
        $data = date("Y-m-d");
    }

    $this->insereDados->$pdo->prepare("INSERT INTO entrada (id_entrada, id_empresa, categoria, subcategoria, valor, forma_pagamento, data) VALUES (?, ?, ?, ?, ?, ?, ?)");
    $this->insereDados->bindValue(1, NULL);
    $this->insereDados->bindValue(2, $id_empresa);
    $this->insereDados->bindValue(3, $cat);
    $this->insereDados->bindValue(4, $subcat);
    $this->insereDados->bindValue(5, $val);
    $this->insereDados->bindValue(6, $forPag);
    $this->insereDados->bindValue(7, $data);
    $this->insereDados->execute();
}
<?

Then, I call the class and the function:

<?php
require_once "con/conexao.php";
require_once "classes/contaEntrada.php";

$entrada = new contaEntrada();

$id_empresa = 1;
$cat = 1;
$subcat = 1;
$val = "100,00";
$forPag = "Blablabla";
$data;

$entrada->insereConta($id_empresa, $cat, $subcat, $val, $forPag, $data);
?>

However, when I run the function, the message "SERVER ERROR 500" appears on the screen. Can anyone help me?

    
asked by anonymous 04.11.2015 / 18:09

1 answer

1

$insereDados does not seem to have a property called pdo nor does the connection, who receives the connection is the variable $pdo it's who should call prepare()

 $this->insereDados->$pdo->prepare("INSERT ....")

Modify the code for:

$pdo = conectar();
//código omitido
$this->insereDados = $pdo->prepare("INSERT INTO ....");

$this->insereDados->bindValue(1, NULL);
$this->insereDados->bindValue(2, $id_empresa);
//demais binds ...

if(!$this->insereDados->execute()){
   print_r($this->insereDados->errorInfo());
}
    
04.11.2015 / 18:24