Error: the right syntax to use near 'add

2

I do not know what's happening but my code does not work it is simply not inserting 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

And here is the error that is displaying on the screen

  

You have an error in your SQL syntax; check the manual that   correspond to your MySQL server version for the right syntax to use   near 'add, hiddentotal, date, output_id1, array_id) values (' 6 ',   '14 .25 ',' 2 ',' 1 'at line 1

    
asked by anonymous 30.10.2015 / 05:45

3 answers

4

The problem is add is a mysql reserved word then it is required escape names of columns or tables with backsticks.

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')"
    
30.10.2015 / 11:10
3

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

2 options for correction:

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

    or

      
  • Change the type of your date column in MySQL from date to datetime , so you can receive the value in the format you are trying to pass.
  •   

    MySQL Reference Manual > Data Types > Date and Time Types

        
    30.10.2015 / 09:47
    2

    Yes, really is due to the use of the reserved word 'add', as for the date, it is convenient 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'.

        
    30.10.2015 / 13:22