Error: the right syntax to use near 'add

Asked

Viewed 221 times

2

I don’t know what’s going on but my code doesn’t work it is simply not entering data into the table

<?php
include "../../lib/inc_con.php";
session_start();
$mesa = $_POST['mesa'];
$tamanho = $_POST['tamanho'];
$quantidade = $_POST['qtd'];
$add = $_POST['add'];
$hiddentotal = $_POST['hiddentotal'];
$data = date('Y-m-d H:i:s');
$produto_id1 = $_POST['produto_id1'];
$atendente_id = $_SESSION['id'];

$sql = mysql_query("INSERT INTO pedidos (mesa, tamanho, qtd, add, hiddentotal, data, produto_id1, atendente_id) values ('$mesa', '$tamanho', '$quantidade', '$add', '$hiddentotal', '$data', '$produto_id1', '$atendente_id')") or die (mysql_error());

?>

if necessary here is my table

TABELA PEDIDOS

And here’s the error that’s showing on the screen

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 'add, hiddentotal, date, producto_id1, atendente_id) values ('6', '14.25', '2', '1' at line 1

  • 1

    by error message you cannot deduce anything?

  • 2

    After a look at this post : http://answall.com/questions/579/por-que-n%C3%A3o-should-use-fun%C3%A7%C3%B5es-type-mysql

  • 1

    use mysqli_* or PDO, mysql* is being discontinued in new versions of PHP 5.4.

  • If you decided not to forget to accept the answer that was most useful to you, also see How and why to accept an answer?

3 answers

4


The problem is add is a mysql reserved word then it is necessary to escape column names or tables with backsticks.

The add is usually seen this way:

ALTER TABLE <nome> ADD COLUMN <nome> ... 

Corrected query:

INSERT INTO pedidos (mesa, tamanho, qtd, `add`, hiddentotal, data, produto_id1, atendente_id)
 values ('$mesa', '$tamanho', '$quantidade', '$add', '$hiddentotal', '$data', '$produto_id1', '$atendente_id')"

3

The error is in the entry of your column date in Mysql. Your column date is of the defined type date, so this type accepts input values in the format yyyy-mm-dd and its variable $data is trying to pass the value of yyyy-mm-dd H:i:s.

2 options for correction:

  1. Change the value of your variable to date('Y-m-d')

or

  1. Change the type of your column date in Mysql of date for datetime, so you can receive the value in the format you are trying to pass.

Mysql Reference Manual > Data Types > Date and Time Types

  • Actually this is indifferent, because the table captures only the values of the date and ignores those of the time, and the error is not accounted for there, is in the fourth value.

2

Yes, it is really due to the use of the reserved word 'add', as to the date, it is appropriate to change the field in the database to datetime, otherwise no error appears but the data that will appear in the database will be - '0000-00-00'.

Browser other questions tagged

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