Seaholme Posted June 30, 2010 Share Posted June 30, 2010 Basically I'm trying to make it so that a number only increases +2 if the submit button is pressed. At the moment, the number increases by 2 no matter what happens -- even just refreshing the page! This is my original php // Make a MySQL Connection mysql_connect(".....", ".....", ".....") or die(mysql_error()); mysql_select_db("....") or die(mysql_error()); mysql_query("UPDATE players SET onhand = onhand + 2 WHERE id=".$_SESSION['id']); // A simple example of a form. echo '<form action=camp.php method=post> <input type=submit name=submit value=Submit> </form>'; } And then this is my attempt to make it into an if statement, so that it only increases when the button is pressed. However, this totally screws with my page (keeps giving me an error in the final line of php: Parse error: syntax error, unexpected $end in /home/content/91/6351791/html/camp.php on line 88) and even if I could work out what the error is, I'm not sure if I'm approaching it correctly to achieve the outcome I want. // A simple example of a form. echo '<form action=camp.php method=post> <input type=submit name=submit value=Submit> </form>'; if ( value == Submit ) { // Make a MySQL Connection mysql_connect("....", "....", "....") or die(mysql_error()); mysql_select_db("....") or die(mysql_error()); mysql_query("UPDATE players SET onhand = onhand + 2 WHERE id=".$_SESSION['id']); } Can anybody suggest how I could solve it so it only adds on 2 when the submit button is pressed? Thanks in advance. Everybody's been really, really helpful so far, and invaluable in my attempts to get a grip on php! Quote Link to comment https://forums.phpfreaks.com/topic/206310-difficult-with-an-if-statement/ Share on other sites More sharing options...
JasonLewis Posted June 30, 2010 Share Posted June 30, 2010 Check out isset. You need to check if the submit button is set. if(isset($_POST['submit'])){ //update table here } Also, your HTML is poorly written and formatted. Quote Link to comment https://forums.phpfreaks.com/topic/206310-difficult-with-an-if-statement/#findComment-1079277 Share on other sites More sharing options...
harristweed Posted June 30, 2010 Share Posted June 30, 2010 If the page is refreshed then the data will update again. Therefore I suggest have the form access a script, the update script is held in a separate file, after a successful update redirect back to the form then refresh of the form will refresh that page and not the update script! Quote Link to comment https://forums.phpfreaks.com/topic/206310-difficult-with-an-if-statement/#findComment-1079279 Share on other sites More sharing options...
aebstract Posted June 30, 2010 Share Posted June 30, 2010 You don't have to redirect them to another script, you can just have it redirect them to the same page after you're done with what you're doing. Then you'll be there on a fresh page. Same concept I suppose, just doesn't use two separate scripts. if (isset($_POST[submit])) { do something } Quote Link to comment https://forums.phpfreaks.com/topic/206310-difficult-with-an-if-statement/#findComment-1079281 Share on other sites More sharing options...
harristweed Posted June 30, 2010 Share Posted June 30, 2010 I agree but have found that separate scripts are easier to manage but that just a personal preference. Quote Link to comment https://forums.phpfreaks.com/topic/206310-difficult-with-an-if-statement/#findComment-1079293 Share on other sites More sharing options...
Seaholme Posted July 1, 2010 Author Share Posted July 1, 2010 Thanks, I managed to get it work via another page, as suggested Quote Link to comment https://forums.phpfreaks.com/topic/206310-difficult-with-an-if-statement/#findComment-1079421 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.