br3nn4n Posted February 20, 2008 Share Posted February 20, 2008 I'm working on a poll script that uses mysql to store the amount of votes each option has. I need it so that it updates the column corresponding to the which option the user picked (which I know how to do), but is there an EASY way (cue: auto_increment) to just add one to whatever number was already there? If not, I'm resigned to having to get the value in the database, do the +1 math, and then UPDATE it to the db. It just seems like it should be easier...like...well, auto_increment lol Thanks!! Quote Link to comment Share on other sites More sharing options...
toplay Posted February 21, 2008 Share Posted February 21, 2008 If you want to add one to an existing column in a table, you simply do: UPDATE table_name SET column_name = column_name + 1 WHERE ... Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted February 21, 2008 Author Share Posted February 21, 2008 oh wow...okay so you can actually do that? I was thinking that might work but didn't try it. So the syntax would be something like: mysql_query("UPDATE poll SET `votechoice1` = '$userchoice' + 1 WHERE pollnumber = '$thisisthepollnumbervariable'") Is that were the + 1 would go? Or would it be inside the 's ? Quote Link to comment Share on other sites More sharing options...
toplay Posted February 21, 2008 Share Posted February 21, 2008 Yes, that's where the add one would go, however, I'm not sure what your $userchoice holds as a value. Based on your update shown, it would be like this: UPDATE poll SET `votechoice1` = `votechoice1` + 1 WHERE ... But I have a feeling that's not what you want or talking about. Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted February 21, 2008 Author Share Posted February 21, 2008 ahhhh...yes, I see exactly what you mean...I threw together that example in around 30 seconds. I was rushing out of class... That's actually EXACTLY what I want, and in fact am going to impliment right now Thank you so much! Quote Link to comment Share on other sites More sharing options...
fenway Posted February 21, 2008 Share Posted February 21, 2008 Just remember that this isn't thread-safe. Quote Link to comment Share on other sites More sharing options...
hannibal Posted February 23, 2008 Share Posted February 23, 2008 Fenway, What do you mean by thread safe? I am assuming you mean if someone else is executing the same query at the same time, something bad will happen? Thanks, J. Quote Link to comment Share on other sites More sharing options...
fenway Posted February 23, 2008 Share Posted February 23, 2008 I simply mean that depending on what you're doing with this afterwards, you have to remember that anything can happen between statements. Quote Link to comment Share on other sites More sharing options...
hannibal Posted February 23, 2008 Share Posted February 23, 2008 Of course. I thought that it was something more sinister! Thanks again for the reply Fenway, you seem to be the man of the moment on here!! On behalf of us all, thank you for a helping hand with your knowledge. J. Quote Link to comment Share on other sites More sharing options...
fenway Posted February 24, 2008 Share Posted February 24, 2008 No problem... sometimes I've seen selects followed by updates (like moving a balance) and it just doesn't work without some extra precautions. Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted February 24, 2008 Author Share Posted February 24, 2008 To everyone, thank you very much and I got it working Quote Link to comment 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.