0

This code is throwing an error i tried to troubleshoot but I'm missing something . Since I wrote it I'm overlooking a syntax error. Any help ?

$kql="
INSERT INTO References (Email, Company1, Person1, Contact1, Company2, Person2, Contact2, Company3, Person3, Contact3, Company4, Person4, Contact4, Company5, Person5, Contact5)
VALUES ( '$company44','$Company', '$Person', '$Contact', '$Company1', '$Person1', '$Contact1', '$Company2', '$Person2', '$Contact2', '$Company3', '$Person3', '$Contact3', '$Company4', '$Person4', '$Contact4')";

The error is

Error: 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 'References (Email, Company1, Person1, Contact1, Company2, Person2, Contact2, Com' at line 1

9
  • 2
    I think it throws an error because References is actually a keyword (atleast for MySQL). Try doing a simple SELECT in the table, if the error still occurs Commented Oct 3, 2018 at 7:27
  • Try to insert the query using phpMyadmin by replacing the variable with a dummy data Commented Oct 3, 2018 at 7:27
  • What DBMS are you using ? MySQL, MS SQL? pgSQL ? Commented Oct 3, 2018 at 7:29
  • @Swellar it's used in MSSQL and pgSQL too Commented Oct 3, 2018 at 7:40
  • 1
    Please do not create SQL queries by joining strings together. You should always use parametrised queries, otherwise you have a significant security risk from SQL injection. Also, using a parametrised form will make your code much more robust. For example, as it stands, if $person was a "Mr O'brien" his name would close your quotation mark in the middle of his name, causing a new syntax error. Commented Oct 3, 2018 at 7:59

1 Answer 1

1

REFERENCES Is a SQL keyword used to define a foreign key.

If you have a table/col named with a SQL keyword, you have to wrap the table/col name into specific characters.

MySQL

INSERT INTO `References` (...) ...

MS SQL

INSERT INTO [References] (...) ...

Postgre SQL

INSERT INTO "References" (...) ...

I'm not sure concerning pgSQL, can someone confirm?


In example, nothing (but common sense) prevents you from creating a database named INSERT with a table INTO having a column VALUE(42)

enter image description here

This query works :

USE [INSERT]
SELECT [INTO].[VALUE(42)] FROM [INTO]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.