Insert function is not working

Asked

Viewed 23 times

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 class and 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 someone help me?

  • In the inseaccount function at the end has a <? is typing error? 500 error or blank screen is always programming error. Activate the errors, so add these two lines from at the beginning of the file, ini_set('display_errors', true); error_reporting(E_ALL); Have other tips on php wiki besides this.

  • This <? is a typo, because I didn’t put it in the code. .

  • Now this two error warning appears: Notice: Trying to get Property of non-object in /Applications/MAMP/htdocs/systems/systems_web/fluxo_de_box/fluxojoin_v_hap/classes/contaEntrada.php on line 31 Fatal error: Call to a Member Funcprepare() on null in /Applications/MAMP/htdocs/systems/systems_web/fluxo_de_box/fluxojoin_v_hap/classes/accountEntrada.php on line 31

  • $insereDados does what? has the connection?

  • Yes, see the code above. If that’s not what you’re asking, sorry, then I don’t get it

1 answer

1


$insereDados doesn’t seem to have a property called pdo nor the connection, who receives the connection is the variable $pdo she’s the one to call prepare()

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

Modify the code to:

$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());
}
  • All right, I guess that’s it.

  • That’s right @rray. I had switched from "=" to "->"

  • What is this for: if(!$this->inserts Ados->execute()){ print_r($this->inserts Ados->errorInfo(); }

  • It will print the error message if one happens. @Gustavosevero

  • And how do I display a message of success or failure in this execution?

  • @Gustavosevero as using exceptions puts the execute() inside of the try-catch it will capture a Pdoexception there you can do the treatment, otherwise it is successful.

  • Okay, I’ll do it. Thanks

Show 2 more comments

Browser other questions tagged

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