0

i got an error running this SQL with PHP, but if i run it on the database itself it works without any error (im still quite new to mysql, so please give me a hint whats wrong, i can not find the problem).

The Query (also only line of code if $SQLDatei):

  INSERT INTO BankUser (ID, Name, Admin, Password, AccessLevel, LoggedIn, FailedLogin, Banned, IP, Browser, LastLogin, Kommentar) VALUES (NULL, 'Luke', 'Luke', 'supersecretpassword', '3', '0', '0', '0', 'Unbekannt', 'Unbekannt', '2016-01-01 00:00:00', '');

The 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 'INSERT INTO BankUser (ID, Name, Admin, Password, AccessLevel, LoggedIn, Faile' at line 1

PHP Code:

foreach($Datenbankorganisation as $SQLDatei) {
   $SQL = file_get_contents($SQLDatei);  
   $Result = mysqli_query($DB_LINK, $SQL);

   if(!$Result) {
      echo "<p>[".$SQLDatei."] <span style='color: red';>SQL-Fehler</span>: ".mysqli_error($DB_LINK)."</p>";
   } else {
    echo "<p>[".$SQLDatei."] <span style='color: green';>SQL erfolgreich</span>.</p>";
   }
}
7
  • If ID auto_increment, you could try to remove ID and NULL from your statement. Commented May 24, 2016 at 13:08
  • I can't remember the specifics but capitalization of databases, tables and columns can cause problems, I am sure if you would provide the PHP code that you use the query with someone might provide you with the exact settings that need to be run in order to allow for capital letters in your queries. Commented May 24, 2016 at 13:09
  • We need to see the query. Show us what is inside $SQLDatei Commented May 24, 2016 at 13:14
  • What is the value of SQLDatei? How are you building the query? Commented May 24, 2016 at 13:14
  • what are you getting by file_get_contents ? Its returning your query ? Commented May 24, 2016 at 13:16

1 Answer 1

1

The 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 'INSERT INTO BankUser […]

MySQL usually shows the part of the statement from the position where it encountered an error after “the right syntax to use near”.

Since it shows your full statement from the beginning here, likely there is something before the INSERT keyword - that you don’t/can’t see, but MySQL does & trips over it.

You are reading your SQL queries from files here, so my first guess would be that those files probably include a Byte Order Mark (BOM), to indicate what character encoding the file content is stored in – most likely that is UTF-8 with BOM. So try and save them as UTF-8 without BOM. (For example Notepad++ can detect the character encoding used, and convert to the corresponding one without BOM easily.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your guess! I changed the coding with notepad, saved and reuploaded my script, its working!

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.