Muiter Muiter - 13 days ago 7
MySQL Question

Error in MySQL INSTERT INTO query

I really do not understand why the query below is giving a error.

$sql = "INSERT INTO telefoonnotitie
(
verzoek_id,
klant_id,
contact_id,
offerte,
order,
factuur,
bestelling,
bericht,
gemaakt_id,
gemaakt,
user_id
)
VALUES
(
'".$verzoek_id."',
'".$klant_id."',
'".$contact_id."',
'".$offerte."',
'".$order."',
'".$factuur."',
'".$bestelling."',
'".$bericht."',
'".$_SESSION['user_id']."',
NOW(),
'".$_SESSION['user_id']."'
)
";


The error is '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 'order, factuur, bestelling, bericht, ' at line 7'

The output of this query is


INSERT INTO terugbellen ( verzoek_id, klant_id, contact_id, offerte,
order, factuur, bestelling, bericht, gemaakt_id, gemaakt, user_id )
VALUES ( '1', '472', '1127', '', '', '6161003', '', 'Dit is een
testbericht', '1', NOW(), '1' )


Any suggestions?

Answer

order is a SQL key-word. Wrap that column name in back ticks, like this:

 $sql = "INSERT INTO telefoonnotitie
    (
        verzoek_id,
        klant_id,
        contact_id,
        offerte,
        `order`,
        factuur,
        bestelling,
        bericht,
        gemaakt_id,
        gemaakt,
        user_id
    )
    VALUES

Suggestion, you should really use Prepared Statements instead of concatenating your queries to eliminate the risk of SQL Injection attacks.

Comments