MySql syntax error

-1

I'm making a form to insert data into my database but the function that does this is returning me a syntax error I can not find.

Function

function insereProduto($conexao, $nome, $descricao, $tamanho, $quantidade)
{
$query = "insert into uniformes (nome, descricao, tamanho , quantidade)
    values ('.$nome.', '.$descricao.', '.$tamanho.', .$quantidade.);";
return mysqli_query($conexao, $query);
}

Error:

O produto Camisa Polo não foi adicionado: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.)' at line 2
    
asked by anonymous 25.07.2017 / 22:23

1 answer

2

The error is in what seems to have done a PHP concatenation within a string :

$query = "insert into uniformes (nome, descricao, tamanho , quantidade) values ('.$nome.', '.$descricao.', '.$tamanho.', .$quantidade.);";

You have defined a double-quoted string and used the concatenation operator, . , within this string . In this way, PHP does not parse such an operator, leaving the dot character in the SQL command, generating the syntax error. You can just replace the dots with keys:

$query = "insert into uniformes (nome, descricao, tamanho , quantidade) values ('{$nome}', '{$descricao}', '{$tamanho}', {$quantidade})";

Indicating PHP to parse the string and replace the variables with their values.

    
25.07.2017 / 22:53