PDO insert only runs once

0

I am studying PDO to be able to redo the functions of a crud that I use in mysqli , but I have a problem, when I do an insert with this function the first time it inserts the value in the database, but if I call function again it only returns false and does not enter the value, this is the function:

public function getConnection() {

    $this->dbConn = null;

    try {
        $this->dbConn = new PDO('mysql:host='.$this->dbHost.';dbname='.$this->dbName, $this->dbuser, $this->dbPass );
        $this->dbConn->exec('Set names utf8');

    } catch (PDOException $e) {
        $e->getMessage();
    }

    return $this->dbConn;
}

function PDOCreate( $table, array $data, $ReturnId = false ) {

    $fields = implode(',', array_keys($data));
    $values = "'".implode("', '", $data)."'";

    $field = "";

    foreach ($data as $key => $value) {
        $field .= "$key=:$key, ";
    }

    $field = trim($field, ', ');

    $query = "INSERT INTO {$table} SET $field";

    $stmt = $this->getConnection()->prepare( $query );

    foreach ($data as $key => &$value) {
        $stmt->bindParam( $key, $value );
    }

    if( $ReturnId && $stmt->execute() ) {
        return $this->dbConn->lastInsertId();
    } else {
        return $stmt->execute();
    }       
}

To call this function I'm doing this: PDOCreate('lojas', array("id" => 2, "nome"=> "teste"));

    
asked by anonymous 23.12.2017 / 23:35

0 answers