joe92 Posted April 27, 2011 Share Posted April 27, 2011 I have trouble with the following mysql command. I am trying to make it so that a user may only edit their post 3 times, after which their post will get locked. The following mysql query throws up the following mysql error. My mysql server version is 5.0.91. 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 'IF editCount > 2 THEN SET editLock = '1'; ELSE editLock = '0'; ' at line 10 $query_edit_post = mysql_query("UPDATE forum_posts SET postSubject = '$post_subject_mysql', lastEditTime = '$editTime', lastEditReason = '$lastEditReason', editCount = editCount + 1, editID = '$u_ID', postText = '$editedPostText' IF editCount > 2 THEN SET editLock = '1'; ELSE editLock = '0'; END IF; WHERE postID = '$post_ID_mysql' "); I need the editLock to update automatically when the user edits the post for the third time. I can achieve this by having another 2 mysql queries directly after this, one fetching the result of the editCount and another updating the lock if its greater than 2, ie 3. However, this seems greatly inefficient if I can just do it with the one above. Any help would be greatly appreciated. Cheers, Joe. Quote Link to comment https://forums.phpfreaks.com/topic/234852-mysql-if-statement-not-working/ Share on other sites More sharing options...
fenway Posted April 27, 2011 Share Posted April 27, 2011 You can't use that kind of flow control in an update statement -- you need: editLock = IF( editCount > 2, '1', '0' ) Quote Link to comment https://forums.phpfreaks.com/topic/234852-mysql-if-statement-not-working/#findComment-1206868 Share on other sites More sharing options...
joe92 Posted April 27, 2011 Author Share Posted April 27, 2011 Worked a treat. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234852-mysql-if-statement-not-working/#findComment-1206883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.