SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast int3rface's Avatar
    Join Date
    Jun 2003
    Location
    Hull
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding numbers 1 + 1 = ?

    is there a way to add to a number in a database. For example the field in the database is for example 5, instead of inserting over it adds to it. Lets say the user selects a number from a pop up which in this case is 3 rather than overwriting the exsisting data php or sql adds the 3 to the five to give a new figure in the database of 8. This would also need to work for negatives.

    Any ideas/advice?

    TIA

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    update table set field = field + #
    where ...
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Enthusiast int3rface's Avatar
    Join Date
    Jun 2003
    Location
    Hull
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers simple when you know how another question I've got the following if statement but it always says that the top scorers exists:

    PHP Code:
    $CID $_POST['CID'];
      
    $MID $_POST['MID'];
      
    $goals $_POST['goals'];

     if (
    'MID' == $MID ) {
              die (
    '<p>Top Scorer already exists, please edit
              in the Top Scorer section or leave it as it is'
    );
            }
            
      
    $sql "INSERT INTO scorers SET
              CID='
    $CID',
              MID='
    $MID',
              goals='
    $goals'
              "

    i've also tried changing the if statement to this:

    PHP Code:
      if (!$goals == '0')    { 
    but then it adds the scorer regardless of if they exist or not. What am I doing wrong?

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure exactly what you are doing, but in these cases I usually do something like:

    Code:
    SELECT count(1) as cnt FROM scorers
    WHERE CID='$CID' and MID = '$MID'
    Then grab the cnt value returned in the row and do:
    PHP Code:
    if ($cnt ) {
     
    'UPDATE...'
    } else {
     
    'INSERT ...'

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •