Insert in MySQL does not execute

-3

I'm having problems in SQL when doing a small insertion in DB. I have a table with 4 fields:

id_match -> int
id_usuario1 -> int
id_usuario2 -> int
situation -> varchar(5)

When I make the following insertion, it does not enter the DB:

INSERT INTO match VALUES(NULL, '$usuario1', '$usuario2', 'open')

What's wrong?

    
asked by anonymous 23.07.2015 / 21:02

2 answers

4

Your mistake is because match is a reserved word. This means that for it to be used as an identifier (column name or table, for example) it needs special handling.

By your comment I saw that you have already solved the problem. However, here is the answer for future reference.

INSERT INTO 'match' VALUES(NULL, '$usuario1', '$usuario2', 'open')

To use a reserved word such as table name, column, or indentifying an index, you can use one of two solutions;

  • Using a grave accent "'", or

  • If ANSI_QUOTES in SQL mode is enabled you can use quotation marks (")

23.07.2015 / 21:55
3

Do this as it should:

INSERT INTO match VALUES(NULL, 5, 0, 'open')

You are trying to write strings a column of type int . It is not possible.

With the edit it looks like it's a PHP code, so the correct one would look something like this:

INSERT INTO match VALUES(NULL, $usuario1, $usuario2, 'open')

But I can not guarantee why information about the PHP code is missing.

Finally there is an error in the table name. From the comment added below it was clear what the error was. AP has solved the "escaping" table name which is a reserved SQL word. This way:

INSERT INTO 'match' VALUES(NULL, $usuario1, $usuario2, 'open')
    
23.07.2015 / 21:09