开发者

Problem with PHP & SQL Query [duplicate]

开发者 https://www.devze.com 2022-12-23 19:54 出处:网络
This question already has an answer here: Syntax error due to using a reserved word as a table or column name in MySQL
This question already has an answer here: Syntax error due to using a reserved word as a table or column name in MySQL 开发者_如何学编程 (1 answer) Closed 8 years ago.

I have a problem in php code inserting values into database (I use PHPMyAdmin).

My DATABASE has 3 tables:

  1. Member with this fields: MemberID, MemberName
  2. Room with this fields: RoomID, RoomName
  3. Join with this fields: MemberID, RoomID

The idea is to join the member in the room.

My query was

mysql_query("INSERT INTO join (RoomID, MemberID) VALUES ('121', '131')");

but unfortunately it does not work.


JOIN is a reserved keyword. You might be able to escape it using backticks

INSERT INTO `join` (RoomID, MemberID) VALUES ('121', '131')

but I would suggest renaming the table if possible.


JOIN is a reserved word in most flavours of SQL.
Try putting the database name before the table name

insert into dbname.join (RoomID,MemberID) ....

Or better yet, rename the table join to something else.


Try putting the table name in backticks:

insert into `join` (RoomID,MemberID) values ('121', '131')

Because "join" is an SQL keyword, the parser will get confused if you try to also use it as a name without explicitly making it a name via the ticks.

In the future, you probably don't want to name tables as things that are already SQL keywords - instead, name them something more descriptive, like "RoomsAndMembers" or some such.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号