简体   繁体   中英

setting a limit to a income every 15 minutes error

ok so this file runs every 15 minutes you get a income, farming and energy bonus at those stages but I don't want them to go over the $storagecap but they don't stop and they keep adding more and more food, gold, energy to your account and never stops.

What I am trying to do is add you income and then if your income goes over $storagecap put your gold to $storagecap but it doesnt work here is my code

<?php 
include("functions.php");
connect();
include("user_stats.php");
?>

 <?php
      if(isset($_SESSION['uid'])){
        include("safe.php");
        include("storagecap.php");?>

<?php
$get_users = mysql_query("SELECT * FROM `stats`") or die(mysql_error());

while($user = mysql_fetch_assoc($get_users)) {
  //Get
  $gold = $user["gold"];
  $food = $user["food"];
  $energy = $user["energy"];

  //Increment
  $gold += $user["income"];
  $food += $user["farming"];
  $energy += 5;

  //Verify and correct
  if($energy > 100)
     $energy = 100;

  if($gold > $storagecap)
     $gold = $storagecap;

  if($food > $storagecap)
     $food = $storagecap;

  //Submit
  $update = mysql_query("UPDATE `stats` SET
                    `gold`= '".$gold."',
                    `food`= '".$food."',
                    `energy`= '".$energy."' WHERE `id`='".$user['id']."'") or     die(mysql_error());
}
?>

Seems to me that you are having some syntax problems in your overflow checks. for one, even if this worked they would have an amount above cap until the next 15 minute round. For example, let say you had 99 energy, and it added 5, you would now have 104. But the cap check would still have the old values, ie 99, and would do nothing. Also

`gold`=`gold` '$storagecap' 

is not correct, you need to use

`gold`= '".$storagecap."'

But to fix the 15 minute delay the best method would be this:

<?php
include("functions.php");
include("storagecap.php");
connect();

$get_users = mysql_query("SELECT * FROM `stats`") or die(mysql_error());

while($user = mysql_fetch_assoc($get_users)) {
  //Get
  $gold = $user["gold"];
  $food = $user["food"];
  $energy = $user["energy"];

  //Increment
  $gold += $user["income"];
  $food += $user["farming"];
  $energy += 5;

  //Verify and correct
  if($energy > 100)
     $energy = 100;

  if($gold > $storagecap)
     $gold = $storagecap;

  if($food > $storagecap)
     $food = $storagecap;

  //Submit
  $update = mysql_query("UPDATE `stats` SET
                        `gold`= '".$gold."',
                        `food`= '".$food."',
                        `energy`= '".$energy."' WHERE `id`='".$user['id']."'") or     die(mysql_error());
}
?>

Please note I have not tested this code, treat it as a rough example

EDIT: Just another note, if this query is run, returning energy of, say 99, and it have to fix this to 100, and in another query running at the same time, some energy I used. This might cause this used energy to be "given back". Sorry, I can't explain it better =P Look into MySQL transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM