kirbyweb Posted November 14, 2009 Share Posted November 14, 2009 <?phpinclude('_______');//calculate money needed for next level of bank upgrade (php part of file)$need_money = 500;?><form>Bank level: <?php echo $username['bank'];?> <br>Next level: <input type="submit" value="<?php echo number_format($need_money);?> money" name="upgradebank"></form><?php//now we create another php part of this fileif ($_POST['upgradebank']) {//we check if user has enough money to upgrade bankif ($username['money'] != $need_money) {//we use die function to stop script execution and to show message what is wrongdie("You don't have enough money to upgrade Bank!");}//everything is OK, we update our user tablemysql_query("UPDATE `castle wars users` SET money=money-'$need_money', bank=bank+'1' WHERE id='$username[id]'") or die(mysql_error());}?> This code is not working when I click the button it does not display the error like it is suppose to please help. Link to comment Share on other sites More sharing options...
Synook Posted November 15, 2009 Share Posted November 15, 2009 You need to specify "post" as the method of the form, it defaults to GET. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 15, 2009 Author Share Posted November 15, 2009 How do I make it so I only die a certain script. Link to comment Share on other sites More sharing options...
Synook Posted November 16, 2009 Share Posted November 16, 2009 What do you mean? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 Like I want to make it so if the money is at 500 it stops all scripts so it stays at 500, but I have another file that has include ('____');And it stops all scripts in that file, how can I make it so it includes the file but it does not stop the scripts in the new file. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 The exit or die statements will immediately stop all execution. If you include a file, it will execute the code in the included file. If you are in an included file and you want to stop execution in the included file and return to the parent file then you can use a return statement. You can use a break statement to escape out of a control structure like a loop. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 I will try a return statement maybe? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 How would I do this in a new file? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 I am using return ('$money'); and nothing changes. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 I'll need to see the code from the different include files to be able to tell what's going on. What you wrote will return the string "$money" to the parent file if it was an include file. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 <?phpinclude ('_____');//calculate money needed for next level of bank upgrade (php part of file)$need_money = 200;?><form method='POST'>Bank level: <?php echo $username['bank'];?> <br>Next level: <input type="submit" value="<?php echo number_format($need_money);?> money" name="upgradebank"></form><?php//now we create another php part of this fileif ($_POST['upgradebank']) {//we check if user has enough money to upgrade bankif ($username['money'] > $need_money) {//we use die function to stop script execution and to show message what is wrongdie("You don't have enough money to upgrade Bank!");}//everything is OK, we update our user tablemysql_query("UPDATE `castle wars users` SET money=money-'$need_money', bank=bank+'1' WHERE id='$username[id]'") or die(mysql_error());}?> Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 <?phpsession_start();$user = $_SESSION['username'];//open my database$connect = mysql_connect('_________', '_______', '____') or die('Couldn\'t connect!');mysql_select_db('_________');$query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'");$username = mysql_fetch_array($query);$last = $username['lastupdate']; //timestamp from db$extraTime = time() - $last;$money = floor($extraTime / 60);if ($username['money']>=500&&$username['bank']<1){die('User\s money 500');}if ($money > 0) { //lets update users table mysql_query("UPDATE `castle wars users` SET lastupdate=lastupdate+$extraTime, money=money+$money WHERE id= '$username[id]'") or die(mysql_error());}echo 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.';?> Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 Bottom is the file that is being included, I want to make it so when the money reaches 500 it displays the text but still shows the form and everything works on the new file. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 Why not just echo the text you want and have the rest of the include content in an if statement?If you want to return from an include file, you need to handle the return somehow, it's the same as with functions. If you have this include file: <?php$var = 'some text';return $var;?> And you include that file like this: <?php$value = include 'file.php';echo $value;?> Then you can use $value to store whatever the include file returned. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 It sounds confusing, any easier ways? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 Should I change my code around?Just need to make it so when the bank is at 0 the money stops at 500. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 It's probably easiest to just use if statements to make sure that the code only runs if the conditions are true. Your code right now exits if the conditions are true, so instead of exiting if things are true, you would only run the rest of the code if the conditions are not true. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 How? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 With if statements:http://www.php.net/manual/en/control-structures.if.php Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 I do not know what you are telling me what I should do. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 Is there an easier way to make the money stop at 500 if the bank is at 0? without die? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2009 Share Posted November 16, 2009 Yes. Use an if statement to only execute the code if the user has less than 500 and the bank is greater than 0. You can even use an else statement to show a message if either of those was not true. That's the easiest way to do it, I don't know how to explain it better than that. Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 you mean smaller then 0 not greater? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 like this if ($username['money']>=500&&$username['bank']<=0){echo 'User\'s money: 500';}else echo 'user\'s money:'. ((int)$username['money']+(int)$money) .'money.'; But then it still updates the db.doesn't it? Link to comment Share on other sites More sharing options...
kirbyweb Posted November 16, 2009 Author Share Posted November 16, 2009 This works but it still adds it in thee db, how do I make it stop? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.