Can a MYSQL field's settings be changed so the value will never go higher than 500000

Discussion in 'PHP' started by x0x, Jul 2, 2008.

  1. #1
    Hello I am not sure if you understood the topic name but let's say, I have a field:

    'amount' which is bigint(20)

    would there be any way to change it so the value can never be greater than 500000 So if a php file tries to add more, it will always stay on 500000.


    ******************

    Another issue, a very important one.
    res int(10) UNSIGNED No 3000

    Usually everything works fine, but suddenly one of my users managed to accidentally get the highest value of the field which is 4294967295
    How can that be? My code is perfect...
     
    x0x, Jul 2, 2008 IP
  2. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #2

    1. Why are you using the data type BINGINT(20) when MEDIUMINT(6) would suffice? Anyway you could use a stored procedure, something like this:
      
      CREATE PROCEDURE UPDATE_AMOUNT(IN iId SMALLINT(1), IN iAmount BIGINT(20))
      BEGIN
          IF ( iAmount > 500000 ) THEN
              SET iAmount = 500000;
          END IF;
      
          UPDATE `users` SET `amount` = iAmount WHERE `id` = iId;
      END/
      
      Code (markup):
      Remember to set the delimiter to '/'. You will also need to replace the table name (users), field name (id) and id declaration (SMALLINT(1)).

    2. You must have a bug somewhere for this to occur, how/when is this field incremented. Do you do range checks of any sort?
     
    clarky_y2k3, Jul 2, 2008 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks for your reply.
    For the 2nd issue:
    here is the code that let's the user take a amount from field 'res' and put it in field 'trn'
    Usually everything runs smoothly but SOMETIMES, very rarely the number explodes to the maximum amount possible. Last time it happened when a user tried to put amount 2000, then the 'res' field would explode to the max amount.. Pretty odd...

    //=================== Stuff ============================================================//
    unset($addedtrnmsg);
    
    if($_POST['reserves']){
    
    $reserves = intval($_POST['reserves']);
    
    $resev = mysql_fetch_array(mysql_query("SELECT res FROM $tab[pimp] WHERE id='$id';"));
    
    if($reserves<1){ $addedtrnmsg = "not a number"; }
    elseif($reserves > $resev[0]){  $addedtrnmsg = "don't have that many"; }
    else{
    
     mysql_query("UPDATE $tab[pimp] SET trn=trn+$reserves, res=res-$reserves WHERE id='$id';");
     $addedtrnmsg = "".commas($reserves)."  exchanged";
    
    }
    
    }
    
    //=================== End of Stuff ======================================================//
    Code (markup):
    They do it using a simple text box and a submit button...
    If you find something weird, please let me know.
     
    x0x, Jul 2, 2008 IP