Jump to content

[SOLVED] MySQL Insert command problem


jharker1987

Recommended Posts

Hi there,

 

I am working on php based system.  I have written a very simple insert command to update a table in my database.  It is refusing to work.  I have written thousands upon thousands of php/mysql lines of code over the years and this is the first time I have had this problem.

 

Here is the code:

$sql = "INSERT INTO '$guid' (DATE_TIME_UPDATE, ACCEPTED, DECLINED, MISSED, REQUESTED, TOTAL) VALUES ('$dt','$accepted','$declined','$missed','$requested','$totaldevices')";

 

 

It looks like this when ran:

INSERT INTO '4c0ac655-41d1-4625-95bb-fe9a339728c5' (DATE_TIME_UPDATE, ACCEPTED, DECLINED, MISSED, REQUESTED, TOTAL) VALUES ('23/10/09 13:11:15','0','0','0','0','0')

 

 

And the error looks like this:

Failed; 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 ''4c0ac655-41d1-4625-95bb-fe9a339728c5' (DATE_TIME_UPDATE, ACCEPTED, DECLINED, MI' at line 1

 

 

its worth noting that I have verified the structure of the table and everything is solid.  Any ideas what the problem is/could be?

Link to comment
Share on other sites

I could just be my screen, but it appears you have single quotes around your table name. These should either be replaced by backticks, or alternatively, since I'm fairly sure 4c0ac655-41d1-4625-95bb-fe9a339728c5 isn't a reserved word ;), just removed.

Link to comment
Share on other sites

Because the table name contains special characters that are not normally permitted in an identifier and it starts with a number, back-ticks would be required.

 

However, since your table name appears to be some form of an id (from $guid) you likely have a serious design flaw.

Link to comment
Share on other sites

I could just be my screen, but it appears you have single quotes around your table name. These should either be replaced by backticks, or alternatively, since I'm fairly sure 4c0ac655-41d1-4625-95bb-fe9a339728c5 isn't a reserved word ;), just removed.

 

using backticks solved the problem! big thanks! this has been really bugging me :)

Link to comment
Share on other sites

using backticks solved the problem! big thanks! this has been really bugging me :)

 

Excellent. Don't forget to mark the topic as solved (button in the bottom left of the screen).

 

Because the table name contains special characters that are not normally permitted in an identifier and it starts with a number, back-ticks would be required.

 

I wasn't aware that '-' was a special character with regards to a table name, I'll bear that in mind for future reference. Just out of interested, I tried testing with a number at the start, it didn't seem to cause an issue without backticks, is this perhaps version dependent?

Link to comment
Share on other sites

The set of alphanumeric characters from the current character set, “_”, and “$” are not special... Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted... Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Anything else would need a back-tick.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.