Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8

Thread: Syntax Error

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Member #
    32332
    I keep running into an issue every time I try and change a group field in my database and I'm having a hard time figuring out what's going on.

    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 'group='1' WHERE id='1'' at line 1
    The code in question is:
    Code:
    		$querychange = mysql_query("UPDATE users SET email='$change_email' group='$change_group' WHERE id='$id'");
    		if ($querychange) {
    			echo "Changes saved!";
    			} else {
    			echo "There was a problem updating the database.";
    			echo mysql_error();
    			}
    It works just fine until I add the group part. I've even added multiple other fields with no problem, but the group just won't work. The form html for that part is a little messy, but here it is:
    Code:
    <?php
    if ($group <= 4) {
    	echo "<tr><td class=\"row1\">";
    	echo "<b>Group</b>";
    	echo "</td><td class=\"row2\">";
    	echo "<select name=\"change_group\">";
    	echo "<option value=\"1\">Education</option>";
    	echo "<option value=\"2\">Office</option>";
    	echo "<option value=\"4\">Boss</option>";
    	echo "<option value=\"5\">Sys Admin</option>";
    	echo "</td></tr>";
    }
    ?>
    Any idea what's going on here?

  2.  

  3. #2
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    You're missing a comma, and if ID is numerical then it shouldn't be surrounded with quotes

    Code:
    UPDATE users SET
    email='$change_email',
    group='$change_group'
    WHERE id=$id
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Member #
    32332
    So I've updated it to:
    Code:
    $querychange = mysql_query("UPDATE users SET email='$change_email', group=$change_group WHERE id=$id");
    I took the quotes off $change_group as well because it's also a number, but I'm still getting the same error.

    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 'group=1 WHERE id=1' at line 1

    I've looked over it and I just can't figure out what's wrong.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    I don't see anything wrong ...
    Are you sure you updated your changes and it's seeing the correct line?
    Maybe you updated the wrong file, or uploaded the "repaired" file into the wrong place?

    $querychange = mysql_query("UPDATE users SET email='$change_email', group=$change_group WHERE id=$id");


  6. #5
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Ah actually I see the issue here. You're using group as a field name but this is a reserved word in MySQL. In this case, you'll want to surround group with backticks, e.g:

    Code:
    UPDATE users
    SET    email='$change_email',
          `group`=$change_group
    WHERE id=$id
    The better solution, however, is to rename the field from group to category for example.
    Zrkonium likes this.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  7. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Member #
    32332
    Quote Originally Posted by Ganners, post: 239577
    Ah actually I see the issue here. You're using group as a field name but this is a reserved word in MySQL. In this case, you'll want to surround group with backticks, e.g:

    Code:
    UPDATE users
    SET    email='$change_email',
          `group`=$change_group
    WHERE id=$id
    The better solution, however, is to rename the field from group to category for example.
    This is perfect you are perfect. I have been fighting with this thing for so long. I changed the name and it works perfectly now!

  8. #7
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Glad I could help
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    doh!
    Those reserved words have gotten me before too!
    I keep forgetting about them.
    Ganners likes this.



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:47 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com