Issue with PHP insert to SQL

Im pretty new to PHP and SQL and I have been following some tutorials. I am trying to insert some simple items into an existing table (and yes the names are exact on the table, login info etc...)

Here is the error I am getting:

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 ''user' ('username', 'email') VALUES ('Testname', '123')' at line 1

Here is my string:

mysql_query("INSERT INTO 'user' ('username', 'email') VALUES ('Testname', '123')") or die(mysql_error());

any ideas?

Answers


there is a difference between ' and ` sign, when you need to call columns you need to cover them with

` sign not with single quote sign '

mysql_query("INSERT INTO `user` (`username`, `email`) VALUES ('Testname', '123')") or die(mysql_error());

Replace your code:

mysql_query("INSERT INTO `user` (username, email) VALUES ('Testname', '123')") or die(mysql_error());

Replace your code to

mysql_query("INSERT INTO user VALUES ('Testname', '123')") or die(mysql_error());

Try this.. Use table name correctly (user).

mysql_query("INSERT INTO user('username', 'email') VALUES('Testname', '123')") or die(mysql_error());

There is a difference in mysql queries between the quote (') and the back-quote (`). The back quote is used to quote names of tables, databases and columns. The normal quote is used to undicate that the given value is a string and not a reference.

so your query should look like

mysql_query("INSERT INTO `user` (`username`, `email`) VALUES ('Testname', '123)")

because "user" is a preserved word as "username" so I put those around back-quotes so mysql knows it's an reference and not a function or property.


in PHP MYSQL Single quote is not use for field name and table name unlike Oracle

You can use

INSERT INTO user (username, email) VALUES ('Testname', '123')

OR

INSERT INTO `user` (`username`, `email`) VALUES ('Testname', '123')

instead of ' single quote Tiled can be used.....

if you dont want to use then its okey just use Tiled for reserve words in query like status or order etc

and as per @Andy said use mysqli driver for connection because mysql_query will bedeprecated in next version


Need Your Help

Ruby-vips > Covert PDF to JPEG

ruby pdf jpeg data-conversion vips

I've been looking through Vips and Ruby-Vips documentation, and I am not seeing anyway to load and convert a PDF. Imagemagick has the convert command in their CLI tools. Is there an equivalent to c...