Mysql syntax error

Asked

Viewed 123 times

-1

I am making a form to insert data in my database but the function that does this is returning me a syntax error that I cannot 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
  • 2

    Swap these points for keys. It looks like you made a very wrong concatenation in your SQL command.

  • I put these points because it was going wrong when I used the keys

  • Bizarre that now was :|

  • 2

    Bizzarro is to wrap a variables with simple quotes and point and $quantity only with hahaha points. I’m not saying it would work but there was no uniformity in the syntax. Wrapping them all in single quotes works too.

  • I tried to put the points because a guy helped me here in the login system and the code had been ' . ' $query = "select * from usuarios where email='".$email."'";

  • 1

    But note that between the single quotes there are still the double quotes, to get out of the string and concatenate. You did not put these double quotes.

Show 1 more comment

1 answer

2


The error is in what seems to have made a PHP concatenation inside a string:

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

You defined a string double quotes and used the concatenation operator, ., within this string. Thus, 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})";

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

Browser other questions tagged

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