2

Why is this query not working? Where's the problem?

The host uses MySql 5.7

INSERT INTO richieste(email, stato, tipo, database, logreg, animazioni, desc, titolo) 
VALUES('1', '2', '3', '4', '5', '6', '7', '8')

Database structure:

id  int(11) No       
email   int(11) No       
stato   int(11) No       
tipo    int(11) No       
database    int(11) No       
logreg  int(11) No       
animazioni  int(11) No       
desc    int(11) No       
titolo  int(11) No 

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 'database, logreg, animazioni, desc, titolo) VALUES('1', '2', '3', '4', '5', '6',' at line 1

How can I fix this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Just remove the trailing comma in the `values()` part of the query. On top of that `database` and `desc` are [reserved words in MySQL](https://dev.mysql.com/doc/refman/8.0/en/keywords.html), so you need to surround them with backticks in the columns list. Voting to close this as a typo. – GMB May 14 '20 at 21:09
  • 1
    Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman May 14 '20 at 21:09
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era and should not be used in new code. You’re mixing the two styles here for no apparent reason. – tadman May 14 '20 at 21:09

1 Answers1

1

Try to avoid using MySQL Reserved Keywords as table or column names. You're using database here, which is a keyword, so it must be escaped, while desc should be as well:

INSERT INTO richieste(email, stato, tipo, `database`, logreg, animazioni, `desc`, titolo)

You'll have to do this every time you reference these columns so if it's not too late, rename them to something that doesn't conflict.

tadman
  • 208,517
  • 23
  • 234
  • 262