Error inserting into MYSQL via PDO

Asked

Viewed 62 times

0

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

This my 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 give the Submit it returns "Could not register the answers. Contact your programmer!

Someone can give me a light?

  • Gives a var_dump($new_fields) and sees if the values are valid and fit with the data types set in the database. You can also connect the errors to see better what the problem is. For this you can use this code at the beginning of the script: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

1 answer

0


As single quotes are not used for table names:

'formulariodadoscadastrais'

You can use:

`formulariodadoscadastrais`

Or not to use:

formulariodadoscadastrais

Crase accents are used only when there are reserved words, space, or some types of non-accepted characters, in normal

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

Should stay 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 execution errors, it should look 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();
}
  • 1

    Perfect William, solved!. Thank you very much.

Browser other questions tagged

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