rondog Posted April 1, 2008 Share Posted April 1, 2008 I have a basic select statement and its throwing a syntax error. All I am doing is updating group name in my DB. I am checking if they submit the same one as well as checking if the one they are submitting is a duplicate which is why I have 2 queries. It is saying: 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 WHERE name = 'ROTC'' at line 1 If I echo out my select statement I get this which looks fine to me: SELECT name FROM group WHERE name = 'ROTC' Here is my code managing this update: <?php if(isset($_POST['savegroupchanges'])) { //$sql2 and $result2 determined if the user changed the title field or not. $groupname = $_POST['groupfield']; $sql = mysql_query("SELECT name FROM group WHERE name = '$groupname'") or die(mysql_error()); $sql2 = mysql_query("SELECT name FROM group WHERE id = '$gid'") or die(mysql_error()); $num = mysql_num_rows($sql); $result2 = mysql_fetch_array($sql2); if($result2['name'] != $groupname && $num > 0) { $statustext = "<p style=\"color:#ff0000;background-color:#fff;text-align:center;\">That name is already taken. Please choose another.</p>"; } else { $sql = mysql_query("UPDATE group SET name = '$groupname' WHERE id = '$gid'") or die(mysql_error()); $statustext = "<p style=\"color:#ffd530;background-color:#6F6161;text-align:center;\">Successfully updated.</p>"; } } ?> $groupname is equal to a input text field in a form that is not shown in this code. Any ideas? Link to comment https://forums.phpfreaks.com/topic/99045-solved-cant-find-my-syntax-error/ Share on other sites More sharing options...
papaface Posted April 1, 2008 Share Posted April 1, 2008 group is a reserved mysql word. Always use backticks ` when using field names etc: mysql_query("SELECT `name` FROM `group` WHERE `name` = '$groupname'"); Link to comment https://forums.phpfreaks.com/topic/99045-solved-cant-find-my-syntax-error/#findComment-506808 Share on other sites More sharing options...
trq Posted April 1, 2008 Share Posted April 1, 2008 Always use backticks ` when using field names etc: I would suggest changing the field name to something that is not reserved. Backticks are not sql complient and if you ever need to migrate to another dbms they are bad habbit to get into. Link to comment https://forums.phpfreaks.com/topic/99045-solved-cant-find-my-syntax-error/#findComment-506809 Share on other sites More sharing options...
rondog Posted April 1, 2008 Author Share Posted April 1, 2008 Ahh I should have known!! I've never had to use GROUP yet, but that makes perfect sense! Link to comment https://forums.phpfreaks.com/topic/99045-solved-cant-find-my-syntax-error/#findComment-506817 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.