Jump to content

Difficult with an if statement....


Seaholme

Recommended Posts

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! :)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.