neptunerain Posted December 2, 2012 Share Posted December 2, 2012 Hello everybody, first of all i need to say that this is my first topic and i need to enfasize that my english is so bad 'cause i'm from Brazil. Ok, now let's talk about my "problem". Well, i'm trying to make a form where the user can click in 2 different buttons and these buttons increase variables that correspond with it. Something like: <?php if(isset($_POST['zelda'])){ $zelda++; } if(isset($_POST['mario'])){ $mario++; } ?> <html> <head></head> <body> <form action="" method="post"> <?php echo "Zelda has" . $zelda . "Votes"; ?> <br> <?php echo "Mario has " . $mario . "Votes"; ?> <br><br> <input type="submit" name="mario" value="Vote in Mario!"> <br><br> <input type="submit" name="zelda" value="Vote in Zelda!"> </form> </body> </html> It doesn't work 'cause the variables keep returning their values to 0 over and over again. My friend sad i could use sessions to keep it with some value when refreshing the page. I'm a beginner so i dont have any idea how to solve this. Oh, i tried using sessions but the page become blank and thats it. Sorry about anything, i just came to here because i think this forum has more users than all from brazil. Bye. Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/ Share on other sites More sharing options...
drunkenMonkey Posted December 2, 2012 Share Posted December 2, 2012 (edited) Do you want to register (and remember) the clicks of all visitors that come to your site, or do you only want to register the clicks that 1 user did? If you want to register the clicks of all visitors then you need a "place on your server" to save your current values. That can be in a database or in a file on your server. If you want to register the clicks off 1 user you can use sessions. The session value is stored on the clients computer and is unique for each visitor. Edited December 2, 2012 by drunkenMonkey Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396950 Share on other sites More sharing options...
neptunerain Posted December 2, 2012 Author Share Posted December 2, 2012 (edited) Alright i understood. In this case the data wont be individual so forget about sessions right? Well, i don't want to use database 'cause there aren't stuff to put in it, just a number. I'll search around the internet how to store the value in a file on my server. Thank you by guide me in the correct way. Edited December 2, 2012 by neptunerain Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396953 Share on other sites More sharing options...
MDCode Posted December 2, 2012 Share Posted December 2, 2012 (edited) Well, i don't want to use database 'cause there aren't stuff to put in it, just a number. That makes absolutely no sense. <?php // connection stuff $sql = mysql_query("SELECT * FROM `Votes`"); $row = mysql_fetch_assoc($sql); $zelda = $row['zelda']; $mario = $row['mario']; if(isset($_POST['zelda'])){ $zelda++; $sql = mysql_query("UPDATE `Votes` SET `zelda` = '$zelda'"); echo "Voted for zelda successfully!"; } if(isset($_POST['mario'])){ $mario++; $sql = mysql_query("UPDATE `Votes` SET `mario` = '$mario'"); echo "Voted for mario successfully!"; } ?> <html> <head></head> <body> <form action="" method="post"> <?php echo "Zelda has" . $zelda . "Votes"; ?> <br> <?php echo "Mario has " . $mario . "Votes"; ?> <br><br> <input type="submit" name="mario" value="Vote in Mario!"> <br><br> <input type="submit" name="zelda" value="Vote in Zelda!"> </form> </body> </html> Edited December 2, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396954 Share on other sites More sharing options...
neptunerain Posted December 2, 2012 Author Share Posted December 2, 2012 I dont know if it makes sense because as i sad before i'm from brazil , my english is so bad. But basically i'm thinking of not using any database to do what i want to do because i just want to store a number and not a large amount of data. Can you understand me now? Sorry guys but its my best. Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396956 Share on other sites More sharing options...
MDCode Posted December 2, 2012 Share Posted December 2, 2012 Either storing in a file or database, you get the same result. Using the same amount of data in each. Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396958 Share on other sites More sharing options...
neptunerain Posted December 2, 2012 Author Share Posted December 2, 2012 (edited) Guy one more question, can i increment a row inside the mysql? like this: mysql_query("UPDATE 'votes' SET 'zelda' = 'zelda++'"); I know its wrong but is there any way to do something like this? Edited December 2, 2012 by neptunerain Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396974 Share on other sites More sharing options...
Christian F. Posted December 2, 2012 Share Posted December 2, 2012 First off: Don't use single quotes around field names, only string values. Secondly, this is the proper syntax for adding to a column: UPDATE `votes` SET `zelda` = `zelda` + 1 Notice that I've used backticks where you've used single quotes. As this is the proper way to escape field names. The backticks are optional, however, unless you're trying to use a reserved word. I prefer to always use them, just to make it 100% certain that I will not have any conflicts with current or future reserved words. Quote Link to comment https://forums.phpfreaks.com/topic/271490-increment-variable-when-click-on-submit/#findComment-1396978 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.