SQL syntax error

Asked

Viewed 1,409 times

1

I come again with a similar error in my SQL syntax. I have tried a few times and so far nothing. If you could give me a hand, I’d appreciate it. Just follow my code:

$sql="INSERT INTO processos   
    (
        processo, 
        numero_beneficio, 
        autor, 
        cpf, 
        nit, 
        data_nascimento, 
        nome_mae, 
        nome_aps, 
        codigo_aps, 
        uf_aps, 
        nome_gex, 
        codigo_gex, 
        data_intimacao, 
        data_distribuicao, 
        data_audiencia, 
        processo_prazo,
        processo_destino,
        assunto,
        cod_assunto,
        localizacao
    )
    VALUES";

foreach($dados as $linha_bd)
{                               

$sql=$sql."(
'".$linha_bd['processo']."',
'0006660000',
'".$linha_bd['autor']."',
'".$linha_bd['cpf']."',
'".$linha_bd['nit']."',
'".$linha_bd['data_nascimento']."',
'".$linha_bd['nome_mae']."',
'".$linha_bd['nome_aps']."',
'".$linha_bd['codigo_aps']."',
'".$linha_bd['uf_aps']."',
'".$linha_bd['nome_gex']."',
'".$linha_bd['codigo_gex']."',
'".$linha_bd['data_intimacao']."',
'".$linha_bd['data_distribuicao']."',
'".$linha_bd['data_audiencia']."',
'".$linha_bd['processo_prazo']."',
'".$linha_bd['processo_destino']."',
'".$linha_bd['assunto']."',
'".$linha_bd['cod_assunto']."',
'".$linha_bd['localizacao']."')";
}
$sql=substr($sql,0,strlen($sql)-1); 

require_once("../frameworks/AS_lib/as_Lib_banco_de_dados.php");
$banco_de_dados=new banco_de_dados();
$banco_de_dados->executa_sql($sql);

Contents of the $sql variable:

INSERT INTO processes ( process, benefit_number, author, Cpf, Nit, data_nascimento, nome_mae, name_aps, codigo_aps, uf_aps, name_gex, codigo_gex, data_intimacao, data_distribuicao, data_audiencia, processo_term, processo_destination, subject, cod_subject, location ) VALUES( '00000022620154036304', '0006660000', 'ROMILDO CARLOS RIBEIRO MENDES', '12585255859', '' '1969-02-22', 'ROSILDA MATOS MENDES', '' '' '' '' '' '2015-03-10', '2015-01-15 13:07:30', '2015-09-21 14:45:00', '2015-04-09', '' '040104 - SPECIAL RETIREMENT (ART. 57/8) - BENEF. IN KIND/ CONCESSION/ CONVERSION/ RESTORATION/ COMPLETION',

Error:

»¿SQLinv¡lido: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '( '00000439020154036304', '0006660000',
'MARCOS DE ALMEIDA', '08782952833', ' at line 44

  • You need to inform what is the error or pq this result is not expected. $sql is formatted correctly print its value to check this. Removing from the case it is possible to simplify this with using a select as values of the Insert.

  • The error that occurs is the following (invalid SQL read: You have an error in your SQL syntax; check the manual that corresponds to your version of the Mysql server for the right to use next syntax), and with this error it does not insert in the database.

  • If Insert has several values it is necessary to separate them by comma and then remove the last with substr.

  • Give a echo of $sql and pole here

  • ready.. I put echo

1 answer

3


When they have n values in a insert, these need to be separated by a comma:

insert into [table_name] (column_1, column_2, ...)
values (value_1, value_2, ...), (value_3, value_4, ...)

In your code you apparently want to do this, you even make a substring to remove the comma after the foreach, but you forgot to concatenate the comma at the end of each set of values:

'".$linha_bd['localizacao']."')";

Change to:

'".$linha_bd['localizacao']."'),";
  • Thanks Luis Henrique, it worked now that I put the comma,

Browser other questions tagged

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