Need help! Simple counter

Discussion in 'PHP' started by MCJim, Jun 24, 2008.

  1. #1
    I'm trying to make a simple counter. I want to make it so if a user clicks '+1' it adds 1 to the counter. If the user clicks '-1' it subtracts 1 from the counter. The data is stored in a MySQL database. Here is what I have so far, it isn't working!:

    <?php if (isset($_POST['id']) && isset($_POST['count'])) {
    		$passed_id = (int)$_POST['id'];
    		$passed_count = (int)($_POST['count']);
    		$getcount = "SELECT count FROM table WHERE id='" . $passed_id . "'";
    		$oldcount = mysql_query($getcount);
    		$newcount = $oldcount + $passed_count;
    		$update_count = "UPDATE table SET count='" . $newcount . "' WHERE id='" . $passed_id . "'";
    		mysql_query($update_count) or die('Error, failed to update count.');
    }?>
    
    <form method="post" action="index.php">
    <center><input type="submit" class="text_button" name="count" value="-1"/>
    <input type="submit" class="text_button" name="count" value="1"/>
    <input type="hidden" name="id" value="<?=$row['id']?>"/>
    </form>
    </html>
    
    Code (markup):
    I'm not sure whats wrong. Any help is greatly appreciated. Thanks in advance! NOTE: My database is properly figured and is accessible (its just not shown), so that isn't the problem.
     
    MCJim, Jun 24, 2008 IP
  2. algaidaman

    algaidaman Active Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #2
    What I would do first is echo out your variables to make sure that they are all being passed. Then start tracing your code from there.
     
    algaidaman, Jun 24, 2008 IP
  3. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I seem to have spotted the problem, the $passed_id echos out as "Resource id #6". This messes everything up. What did I write wrong in regards to $passed_id?
     
    MCJim, Jun 24, 2008 IP
  4. algaidaman

    algaidaman Active Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #4
    try changing your code from:
    $passed_id = (int)$_POST['id'];
    Code (markup):
    to:
    $passed_id = int($_POST['id']);
    Code (markup):
    and do the same with the other variables.

    More Info
     
    algaidaman, Jun 24, 2008 IP
  5. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That ended up making it worse and made it stop working completely. Thanks for trying to help though. I'm still trying to figure this out..

    EDIT: I think the ID is working ok actually. Its the $oldcount = mysql_query($getcount); that isn't working.
     
    MCJim, Jun 24, 2008 IP
  6. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Im not sure about this, but isn't count a reserved word for mysql? I am not sure but I think using count as a column name for your table is a good idea... try changing the column name for that and see if it helps.. I had problems before where I named a column in my database as desc, which is a reserved word for mysql..
     
    bokiatenxi, Jun 24, 2008 IP
  7. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #7
    
    <?php
    if (isSet($_POST['id']) && is_numeric($_POST['id']) && isSet($_POST['count']))
    {
        $passed_id    = $_POST['id'];
        $passed_count = ($_POST['count']);
        $getcount = "SELECT `count`
                     FROM table
                     WHERE id = '" . $passed_id . "'";
        $query = mysql_query($getcount);
        $items = mysql_num_rows($query);
        
        if ($items > 0)
        {
            list($oldcount) = mysql_fetch_array($query);
    
            $newcount = $oldcount + $passed_count;
            $update_count = "UPDATE table
                             SET count = '" . $newcount . "'
                             WHERE id  = '" . $passed_id . "'";
            mysql_query($update_count) or die('Error, failed to update count.');
        }
        
        /*
         OR YOU COULD DO if your $_POST['count'] contains similair things like this (-1), (+1)
        */
        
        $cnt = ($_POST['count'] < 0) ? "-1" : "+1";
        $update_count = "UPDATE table
                         SET count = count " . $cnt . "'
                         WHERE id  = '" . $_POST['id'] . "'";
        mysql_query($update_count) or die('Error, failed to update count.');
    }
    ?>
    
    <form method="post" action="index.php">
    <center><input type="submit" class="text_button" name="count" value="-1"/>
    <input type="submit" class="text_button" name="count" value="1"/>
    <input type="hidden" name="id" value="<?=$row['id']?>"/>
    </form>
    </html>
    
    Code (markup):
    Please try to maintain a good looking way of writing your scripts, like my example, you can see 1 one view what's happening...

    Hopefully you understand what i've changed... if not, look under here

    the COUNT word is an reserved mysql word, if you like to use it add `(tilde) to it `count`
    if your input is a number please check it with is_numeric
    I've changed your MySQL query style (easier to read)
    use mysql_num_rows to check if you had any results
    extract with list an variabel from an array (with you see being loaded from MySQL)
    mysql_fetch_array for reading output of mysql

    More questions? please ask ;)
     
    EricBruggema, Jun 25, 2008 IP
  8. algaidaman

    algaidaman Active Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #8
    I hope everything works out for you using EricBruggema advice. But note that (int)var is the wrong way to use the int() function.
     
    algaidaman, Jun 25, 2008 IP
  9. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks a lot, that code worked out! :)

    Thanks, I'll take note of that.
     
    MCJim, Jun 25, 2008 IP
  10. David Pankhurst

    David Pankhurst Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #10
    You can bypass a lot of this if you don't need the old count value - just increment it directly in mysql and save the extra call:

    <?php
    if (isset($_POST['id']) && isset($_POST['count'])) 
    {
      $passed_id = (int)($_POST['id']);
      $passed_count = (int)($_POST['count']);
      $update_count = "UPDATE table SET count=count+($passed_count) WHERE id='$passed_id' LIMIT 1"
      mysql_query($update_count) or die('Error, failed to update count.');
    }
    ?>
    PHP:
    (I like adding the LIMIT clause 'just in case')

    Besides saving one mysql call, you do what is called an atomic (all at once) update - in the first code, if anyone called an update from when you read the counter to when you wrote the update back, you could have invalid values.
     
    David Pankhurst, Jun 28, 2008 IP