galvin Posted February 18, 2011 Share Posted February 18, 2011 If you have a simply query like the one below, and you want to set an id equal to itself PLUS 1, is there an easy way to do that? For example, if the field "someid" in the mysql table was equal to 6, I'd want the code below to set it equal to 7. The part in parentheses is what I need help with. Or is it possible that is actually how i would do it? The value of someid would change a lot, hence my question. My first thought was that I'd have to grab the data first via a separate query and then put the someid value into a variable called $number and then use "($number + 1)" in the next query. But I feel like there has to be a quicker way to simply increase an id by 1. $sql = "UPDATE answers SET someid=(someid +1) WHERE something=$something"; Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/ Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 What happened when you tried it? Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175912 Share on other sites More sharing options...
galvin Posted February 18, 2011 Author Share Posted February 18, 2011 it didn't work, but I have a ton of other code on that page that could be screwing it up. I guess I should create a new file with just the relevant snippet and test it. Sorry, I'll do that now... Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175914 Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 The exact way you have it won't work because you'd have to end the quotes around the variable name, use + around the varname, then enclose the rest of the query in quotes. Also, javascript isn't like php--you can't just hit return and continue a string on the following line. To concatenate a string variable you have to a trailing space and a backslash: alert("This is a sample string, and although I'm coding / in multiple lines, this will all be put together in / the alert!"); I'm assuming this is for javascript and not for php, since it's in the javascript forum. Tecnically you shouldn't use $ at the start of a variable name in javascript--the $ is reserved for machine-generated code. I know you *can* but you shouldn't. My guess is that you're doing it for the same reasons I used to: to bridge a gap between php and javascript when working with AJAX. Instead, I recommend headlessCamelCase--here's a good explanation as to why: http://www.bennadel.com/blog/1760-Five-Months-Without-Hungarian-Notation-And-I-m-Loving-It.htm And don't forget to follow the specific headlessCamelCase link :wink: http://www.dustypixels.com/downloads/headless_camel_case_its_a_beautiful_thing.jpg Anywho, your code would probably be more stable in the long term if you set the variable value first (and imho, it's easier to read and see what's going on...) queryIdValue = idValue++; //Same as idValue + 1; var sql = "UPDATE answers SET someid= "+queryIdValue / +"WHERE something = "+somethingValue; Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175915 Share on other sites More sharing options...
galvin Posted February 18, 2011 Author Share Posted February 18, 2011 I'm such an idiot, I meant to put this in the MySQL forum. Ugh, I'm having a bad day. And sorry to make you write all that because of my idiotic mistake. Can someone with the ability to move this, please do so. I don't see the "move" option. I think I need to just call it day at this rate Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175917 Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 Moved . . . Didn't notice it was JS help earlier. Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175924 Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 Well, then I'm assuming that's a php variable you're using for $sql. In that case, you're missing the $ before someid. Although I'd still set the value before running the query. And you'd be better of doing it the same way, just using php notation. $queryIdValue = idValue++; //Same as idValue + 1;var $sql = "UPDATE answers SET someid= '$queryIdValue' WHERE something = '$somethingValue'"; $result = mysql_query($sql); Always works for me. Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175926 Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 I'm under the impression the OP wants to increment the value that is already in the `someid` field in the DB table. If that's the case, then SET `someid` = `someid` + 1 is the correct syntax. Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175929 Share on other sites More sharing options...
jcanker Posted February 18, 2011 Share Posted February 18, 2011 You're probalby right, since OP said it should be in the MySQL forum. I'm just going by the $ preceeding a variable name. Regardless, I should point out an alteration in my above example: The single quotes around the variable shouldn't be needed when the variable doesn't represent a string. Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175931 Share on other sites More sharing options...
galvin Posted February 18, 2011 Author Share Posted February 18, 2011 Don't fret, I'm solely to blame for any confusion in the post I noticed Pikachu used backticks. I knew they were necessary if ever you used a "reserved" word in your queries, but is it good practice to ALWAYS use backticks (whether the word is reserved or not)? Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175934 Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 Regarding the backticks, it depends whom you ask. Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175937 Share on other sites More sharing options...
galvin Posted February 18, 2011 Author Share Posted February 18, 2011 Well what would Zuckerberg say if I asked him? I'll follow your lead Pikachu (since you are obviously an awesome programmer...shameless kissing up, but it's true) and start using backticks all them time Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1175943 Share on other sites More sharing options...
mikosiko Posted February 18, 2011 Share Posted February 18, 2011 I agree with Pikachu... backticks??..... depend to whom you ask.... in my case... I will say... DEPEND... if you are designing/programming ONLY for MYSQL worth to be in the safe side and use the backticks... but if you are programing to eventually point to a different DB (different SQL) then backticks will be a PITA... personally I never use them and apply extra caution to choose my tables and field names. just my 0.2cents Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1176015 Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2011 Share Posted February 18, 2011 That's a good point that mikosiko makes, backticks are specific to MySQL. I almost always use them in coding examples regarding MySQL issues so there's no question whether I'm referring to a field/table name or a value. Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1176028 Share on other sites More sharing options...
kickstart Posted February 18, 2011 Share Posted February 18, 2011 Hi My feeling with back ticks is that I shouldn't be using a reserved word as a column or table name, and back ticks will just disguise me making such a mistake which will be a pain for ever after. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/228046-increase-an-id-by-1-quickly/#findComment-1176159 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.