PHP PDO does not insert value

Asked

Viewed 254 times

0

I don’t really understand PHP, but I’m trying to create a PDO connection. Connection works fine, but does not insert values...

follows the code:

config

define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "root");
define("DB_NAME", "everton");

define("DB_DSN_MYSQL", 'mysql:host='.DB_HOST.';dbname='.DB_NAME);
define("DB_DSN_FIREBIRD", 'firebird:host='.DB_HOST.';dbname='.DB_NAME);

date_default_timezone_set("America/Sao_Paulo");

connection

try {
            $this->con = new PDO(DB_DSN_MYSQL, DB_USER, DB_PASSWORD);
        } catch (PDOException $exception){
            $flag['flag'] = 'CONN_FAILED';
            die(json_encode($flag));
        }

insertion

$sql = 'INSERT INTO teste (codigo, nome) VALUES (:codigo, :nome)';
    $stmt = $this->con->prepare($sql);
    $codigo = 1;
    $nome = 'teste';

    $stmt->bindValue(':codigo', $codigo);
    $stmt->bindValue(':nome', $nome);
  • 1st Prepare, 2nd Bind, 3rd ??? Execute.

1 answer

2

$stmt->execute();

$sql = 'INSERT INTO teste (codigo, nome) VALUES (:codigo, :nome)';
$stmt = $this->con->prepare($sql);
$codigo = 1;
$nome = 'teste';

$stmt->bindValue(':codigo', $codigo);
$stmt->bindValue(':nome', $nome);
$stmt->execute();

Browser other questions tagged

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