I do not understand where I am wrong. You have an error in your SQL syntax; check it out.

$result = mysql_query("INSERT INTO 'persons' VALUES ($idInput, $nameInput, $lnameInput, $ageInput)" , $db) or die(mysql_error()); 
  • 3
    "INSERT INTO persons VALUES ('$idInput', '$nameInput', '$lnameInput', '$ageInput')" - Mr. Black
  • @Doofy around numeric values ​​do not need quotes, when inserting a set of data (say, 50mb or more), they really interfere and the smaller the length of the inserted value, the greater the overhead. - strangeqargo
  • @strangeqargo, and if in the structure of the table the integer is written to varchar? - Mr. Black
  • @Doofy well, if the structure of the table requires it, of course, yes, but it is better to try to avoid it. If the login field accepts both numbers and letters, then yes, but the numeric field is better not (if you write requests yourself, not orm) - strangeqargo
  • one
    @strangeqargo, this should be added to the answer. Ie if in the field structure should include integer, quotes are not needed for the number - Mr. Black

1 answer 1

quotes around string values ​​who will put? and put MySQL Workbench, it automatically highlights the line with incorrect syntax, and debug your queries

ps and you have another error (the root cause, by the way) - single quotes around the table name

 mysql> use 'db'; #здесь корректно, но излишне, это внутренняя команда mysql # хотя use db тоже сработает (и быстрее печатать) Database changed mysql> show tables; +--------------+ | Tables_in_db | +--------------+ | OrderHistory | | users | +--------------+ 2 rows in set (0.00 sec) #здесь некорректно, не по стандарту SQL mysql> select * from 'users'; ERROR 1064 (42000): 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 ''users'' at line 1 

firstly, in this query, quotes around the table name are not needed, and secondly, the correct quotes for sql are backticks:

 mysql> select * from `users`; +----+------------+- | id | login 

The answer to the comments regarding quotes around INT / DOUBLE / FLOAT type fields, etc .: they are possible, but not necessary. With a small load, the extra baytik spent on transferring quotes is imperceptible.

With a large load, when inserting large amounts of data (when you insert data up to max_allowed_packet ), when you have a lot of numeric fields, you will receive an additional load / have to reduce the number of rows that you can insert simultaneously.

In addition, purely semantically, it makes it clear when you look at any big query that the numbers should be inserted into a column with a numeric type.

When you see such a request, you understand that there is logic and intention in it.

  • one
    `are the correct quotes only for MySQL! For SQL, the standard is "users". In MySQL, they are disabled by default, are enabled via ANSI_QUOTES in sql_mode - Minor
  • thanks for the clarification - strangeqargo