How to UPDATE an integer field by adding a specific number?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Breana
    New Member
    • Aug 2007
    • 117

    How to UPDATE an integer field by adding a specific number?

    Ok, i got my point system working had to creat a new php for it the old page was killing the code session end lol.
    But i am stuck, it don't seem to add the value it just replaces the old one..
    so the old value, 5 after update 5 points new value 5 not 10.

    So how do i make it so it adds the new value or reads the old one then adds plus 5?
    old...5+ new...5= 10, or if the user has a value of 200, and it adds 5 it will be 205!

    [PHP]<?php
    $sql = "UPDATE users points = '$points' where userid = $uid";
    $result = mysql_query($sq l ,$db);

    $sql = "delete from ingredients where userid = $uid";
    $result = mysql_query($sq l ,$db);

    printf("<p><fon t face=arial size=-1><br /><br />Account Updated!<br /> 5 Points Recived...</font></p>");
    ?>[/PHP]
  • gregerly
    Recognized Expert New Member
    • Sep 2006
    • 192

    #2
    Originally posted by Breana
    Ok, i got my point system working had to creat a new php for it the old page was killing the code session end lol.
    But i am stuck, it don't seem to add the value it just replaces the old one..
    so the old value, 5 after update 5 points new value 5 not 10.

    So how do i make it so it adds the new value or reads the old one then adds plus 5?
    old...5+ new...5= 10, or if the user has a value of 200, and it adds 5 it will be 205!

    [PHP]<?php
    $sql = "UPDATE users points = '$points' where userid = $uid";
    $result = mysql_query($sq l ,$db);

    $sql = "delete from ingredients where userid = $uid";
    $result = mysql_query($sq l ,$db);

    printf("<p><fon t face=arial size=-1><br /><br />Account Updated!<br /> 5 Points Recived...</font></p>");
    ?>[/PHP]
    If you want to add points to an existing database value you could do something like:

    [PHP]<?
    $sql="UPDATE users SET points = points + $points WHERE userid = $uid";
    ?>[/PHP]

    The above query assumes that $points is already set and is an integer.

    Hope that helps

    Greg

    Comment

    • Breana
      New Member
      • Aug 2007
      • 117

      #3
      Yeah it works thanks, i tried the + idea but crashed the page when i did it but i do see where i went wrong lol thanx!

      Comment

      • gregerly
        Recognized Expert New Member
        • Sep 2006
        • 192

        #4
        Originally posted by Breana
        Yeah it works thanks, i tried the + idea but crashed the page when i did it but i do see where i went wrong lol thanx!
        Not a problem. Glad I could help!

        Greg

        Comment

        Working...