Error inserting into MYSQL via PDO [closed]

0

I recently started in the area and I came across this situation.

This code is not inserting in my MySQL.

 <?php
try{
$pdo = new PDO('mysql:host=localhost:3306;dbname=formularioecofin', 'SECRETO', 'SECRETO');
}
catch (PDOException $e )
{
    echo 'Erro ao conectar com o MySQL: ' . $e->getMessage();
}

? >

  $inserir_banco = $pdo->prepare("INSERT INTO 'formulariodadoscadastrais' (CNPJ, RazaoSocial,  NomeFantasia, NomeResponsavel, Cpf, Telefone,CelularWhatsapp, Email) SET (?,?,?,?,?,?,?,?)");

        $array_sql = array(
        $novos_campos['nCNPJ'],
        $novos_campos['nNome'],
        $novos_campos['nFantasia'],
        $novos_campos['nNomeResp'],
        $novos_campos['nCPF'],
        $novos_campos['nTelefone'],
        $novos_campos['nCelular'],
        $novos_campos['nEmail']
    );

    if($inserir_banco->execute($array_sql)){
        $respostas['erro'] = 'nao';
        $respostas['msg'] = 'Respostas cadastradas com sucesso!';
    }else{
        $respostas['erro'] = 'sim';
        $respostas['getErro'] = 'Não foi possível cadastrar as respostas. Contate seu programador!';
    }
};

Every time I submit it, it returns "It was not possible to register the answers. Contact your programmer!

Can someone give me a light?

    
asked by anonymous 24.09.2017 / 15:44

1 answer

0

single quotes are not used for table names:

'formulariodadoscadastrais'

You can use:

'formulariodadoscadastrais'

Or do not use:

formulariodadoscadastrais
  

Chromatic accents are used only when there are reserved words, space, or some non-accepted character types in normal

Another thing the command INSERT INTO does not use SET the correct would be VALUES

It should look like this:

 INSERT INTO formulariodadoscadastrais (CNPJ, RazaoSocial, NomeFantasia, NomeResponsavel, Cpf, Telefone, CelularWhatsapp, Email) VALUES (?,?,?,?,?,?,?,?)

Being PDO you can add

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

So you can handle runtime errors, something like:

<?php
try {
    $pdo = new PDO('mysql:host=localhost:3306;dbname=formularioecofin', 'SECRETO', 'SECRETO');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $inserir_banco = $pdo->prepare("INSERT INTO formulariodadoscadastrais (CNPJ, RazaoSocial, NomeFantasia, NomeResponsavel, Cpf, Telefone, CelularWhatsapp, Email) VALUES (?,?,?,?,?,?,?,?)");

    $array_sql = array(
        $novos_campos['nCNPJ'],
        $novos_campos['nNome'],
        $novos_campos['nFantasia'],
        $novos_campos['nNomeResp'],
        $novos_campos['nCPF'],
        $novos_campos['nTelefone'],
        $novos_campos['nCelular'],
        $novos_campos['nEmail']
    );

    $inserir_banco->execute($array_sql);
} catch (PDOException $e) {
    echo 'Erro ao conectar com o MySQL: ' . $e->getMessage();
}
    
27.09.2017 / 05:25