0

I'm not being able to do INSERT into my data base and the erro message is quite strange (at least to me). As many who seek for an answer here, I'm very new to PHP and programming in general.

It connects to the data base fine, in other pages it also pulls out more data fine.

As $i runs, it changes $_SESSION['codigo'.$i] and $_SESSION['qtd'.$i]. These are the variables that are most interesting to me.

See some of the code bellow.

$i=1;

while ($i<=$_SESSION['preenchidos']){


$sql_salvarBPA = "INSERT INTO `bpa`
(`Tipo`, 
`ESPECIALIDADE`, 
`PROCEDIMENTO`, 
`QUANTIDADE`, 
`CIDADE`, 
`DATA`, 
`NOME`, 
`CARTAOSUS`, 
`SEXO`, 
`DATA_NASCIMENTO`, 
`VIA`, 
`ENDERECO`, 
`NUMERO`, 
`COMPLEMENTO`, 
`BAIRRO`, 
`Data_input`, 
`Usuario_IP`) 

VALUES (
".$_SESSION['tipo'].",
".$_SESSION['especialidade'].",
".$_SESSION['codigo'.$i].",
".$_SESSION['qtd'.$i].",
".$_SESSION['cidade'].",
".$_SESSION['dataatendimento'].",
".$_SESSION['medico'].",
".$_SESSION['cartaosus'].",
".$_SESSION['sexo'].",
".$_SESSION['datanascimento'].",
".$_SESSION['via'].",
".$_SESSION['endereco'].",
".$_SESSION['numero'].",
".$_SESSION['complemento'].",
".$_SESSION['bairro'].",
".$_SESSION['datainput'].",
".$_SESSION['origem'].")";

if (mysqli_query($conectar, $sql_salvarBPA)) {
echo "New record created successfully<br><br>";
} else {
echo "Error: " . $sql_salvarBPA . "<br>" . mysqli_error($conectar)."<br><br>";
}

$i++;

}

I get this below error message (lines are broken for better reading). Its is very strange that it breaks one of the variables and stops in the middle of the Mysql INSERT statement. I've checked if it is a simple quotation mark mistake, but it doesn't seem to be the case, since it pulls and recognizes the results of the QUERY.

 Error: INSERT INTO `bpa` (`Tipo`, `ESPECIALIDADE`, `PROCEDIMENTO`, 
`QUANTIDADE`, `CIDADE`, `DATA`, `NOME`, `CARTAOSUS`, `SEXO`, 
`DATA_NASCIMENTO`, `VIA`, `ENDERECO`, `NUMERO`, `COMPLEMENTO`, `BAIRRO`, 
`Data_input`, `Usuario_IP`) VALUES ( MEDICO, ULTRASSONOGRAFIA, 
02.05.01.004-0, 1, city name, 2018-06-12, doctor name, 
415, masculino, 2018-05-30, S, S, 1, S, S, 12/06/2018 - 10:28:32am, IP 
Cliente: ***********)

Erreur de syntaxe pr�s de '.01.004-0, 1, city name, 2018-06-12, doctor name, 415' � la ligne 23

Error: INSERT INTO `bpa` (`Tipo`, `ESPECIALIDADE`, `PROCEDIMENTO`, 
`QUANTIDADE`, `CIDADE`, `DATA`, `NOME`, `CARTAOSUS`, `SEXO`, 
`DATA_NASCIMENTO`, `VIA`, `ENDERECO`, `NUMERO`, `COMPLEMENTO`, `BAIRRO`, 
`Data_input`, `Usuario_IP`) VALUES ( MEDICO, ULTRASSONOGRAFIA, 
02.05.02.008-9, 1, city name, 2018-06-12, doctor name, 
415, masculino, 2018-05-30, S, S, 1, S, S, 12/06/2018 - 10:28:32am, IP 
Cliente: ****)

Erreur de syntaxe pr�s de '.02.008-9, 1, city name, 2018-06-12, doctor name, 415' � la ligne 23

This is how my Mysql data base table is configured:

1   Tipo            mediumtext          utf8_unicode_ci
2   ESPECIALIDADE   mediumtext          utf8_unicode_ci
3   PROCEDIMENTO    text                utf8_unicode_ci
4   QUANTIDADE      int(11) 
5   CIDADE          mediumtext          utf8_unicode_ci
6   DATA            date    
7   NOME            text                utf8_unicode_ci
8   CARTAOSUS       text                utf8_unicode_ci
9   SEXO            text                utf8_unicode_ci
10  DATA_NASCIMENTO date    
11  VIA             text                utf8_unicode_ci
12  ENDERECO        text                utf8_unicode_ci
13  NUMERO          int(11) 
14  COMPLEMENTO     text                utf8_unicode_ci
15  BAIRRO          text                utf8_unicode_ci
16  Data_input      timestamp   
17  Usuario_IP      text                utf8_unicode_ci
5
  • 2
    Learn about prepared statements to prevent SQL injection Commented Jun 12, 2018 at 14:04
  • Add single quotes arrount non numeric values Commented Jun 12, 2018 at 14:04
  • Most of the database values should be in quotes, something that prepared statements helps solve. Commented Jun 12, 2018 at 14:05
  • 1
    $_SESSION['codigo'.$i] - stop nonsense like this, and use arrays instead. Commented Jun 12, 2018 at 14:09
  • @CBroe thanks for your honesty. I will definetely consider that for this project in the future. Commented Jun 12, 2018 at 14:13

2 Answers 2

2

String type values should be enveloped in single quotes ('')

so instead of

".$_SESSION['tipo'].",

try

'".$_SESSION['tipo']."',

and so on

Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying it. Do you think it is responsible for that error display? Because it reads variables and results normally.
It's displaying a syntax error. Since this is a syntax error if this doesn't resolve the problem it will at least resolve part of it
@Yeeter i get it. Thanks, man! It really helped me to understand it better.
1

replace ur query $sql_salvarBPA = statement to this

add quotes ' to datatypes other than int

**$sql_salvarBPA = "INSERT INTO `bpa`
(`Tipo`, 
`ESPECIALIDADE`, 
`PROCEDIMENTO`, 
`QUANTIDADE`, 
`CIDADE`, 
`DATA`, 
`NOME`, 
`CARTAOSUS`, 
`SEXO`, 
`DATA_NASCIMENTO`, 
`VIA`, 
`ENDERECO`, 
`NUMERO`, 
`COMPLEMENTO`, 
`BAIRRO`, 
`Data_input`, 
`Usuario_IP`) 
VALUES (
'" . $_SESSION['tipo'] . "',
'" . $_SESSION['especialidade'] . "',
'" . $_SESSION['codigo' . $i] . "',
" . $_SESSION['qtd' . $i] . ",
'" . $_SESSION['cidade'] . "',
'" . $_SESSION['dataatendimento'] . "',
'" . $_SESSION['medico'] . "',
'" . $_SESSION['cartaosus'] . "',
'" . $_SESSION['sexo'] . "',
'" . $_SESSION['datanascimento'] . "',
'" . $_SESSION['via'] . "',
'" . $_SESSION['endereco'] . "',
" . $_SESSION['numero'] . ",
'" . $_SESSION['complemento'] . "',
'" . $_SESSION['bairro'] . "',
'" . $_SESSION['datainput'] . "',
'" . $_SESSION['origem'] . "')";**

2 Comments

I'm trying it. Do you think it is responsible for that error display? Because it reads variables and results normally.
Works smoothly! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.