1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

PHP increment help

Discussion in 'Web Design & Development' started by DrP, Nov 28, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    Could anyone help with this? The user hits a link which contains the $id and $action (the latter of which is 'increase' and calls this script). It is supposed to take marblecount, add 1 to it, and replace the original value with the new one.
    However, if the beginning marblecount is 0, the new figure is not 1, as expected, but 2!
    Why?

    // Increase marble count
    case "increase":
    $sql = mysql_query("SELECT marblecount FROM marbles_collection WHERE id = '$id'") or die(mysql_error());
    $row = mysql_fetch_array($sql);
    extract($row);
    $marblecount++;
    $update = mysql_query("UPDATE marbles_collection SET marblecount = '$marblecount' WHERE id = '$id'") or die(mysql_error());
    $redirect = "marbles_admin.php";
    header("Refresh: 1.5; URL=$redirect");
    include "includes/header.inc.php";
    echo "<h3>Marbles Admin Page</h3>";
    echo "<p>You have successfully made a marble available for reward.</p>";
    echo "<p>You are now being redirected to the Marbles Admin Page...</p>";
    include "includes/footer.inc.php";
    break;

    I tried doing it a different way and using this alternative (which is shorter):
    case "increase":
    $update = mysql_query("UPDATE marbles_collection SET marblecount = marblecount + 1 WHERE id = '$id'") or die(mysql_error());
    $redirect = "marbles_admin.php";
    header("Refresh: 1.5; URL=$redirect");
    include "includes/header.inc.php";
    echo "<h3>Marbles Admin Page</h3>";
    echo "<p>You have successfully made a marble available for reward.</p>";
    echo "<p>You are now being redirected to the Marbles Admin Page...</p>";
    include "includes/footer.inc.php";
    break;
    But it still goes from 0 to 2! On subsequent runs it goes from 2 to 3, then 3 to 4, then 4 to 5, and so on (as it should!).
     
  2. JonathanAnon

    JonathanAnon Banned

    Joined:
    Dec 17, 2004
    Messages:
    531
    Without seeing all of the code it is hard to say.
    Why dont you echo out the value of marblecount at every stage and see what the story is.
    That is what I usually do when there is ambiguity over why a value is not as it seems.

    Try
    echo("This is value of marblecount before the SQL statement: ");
    echo $marblecount;

    etc etc.
     
  3. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    I've tried that. Using the first example of code above:
    Echo $marblecount immediately after the SELECT query displays 1.
    Echo $marblecount immediately after the UPDATE query displays 2.

    The thing is that prior to the script being run, the $marblecount is not 1! It's 0! I know this from looking at the Mysql tables using phpMyAdmin.
     
  4. JonathanAnon

    JonathanAnon Banned

    Joined:
    Dec 17, 2004
    Messages:
    531
    It should be easy enough to get this solved. Is there anywhere that you can look at the MySQL valued in the database - i.e. can you use PHPMyAdmin or just connect to the SQL database to view the values to make sure that they are the same as the values that you pull with the SQL query.

    For example if you are pulling marblecount for id=0, look in the database and make sure that the value in the actual database is as expected, then echo the marblecount after each statement in the PHP code. Then view the value in the database after the update command.
     
  5. Eriksrocks

    Eriksrocks

    Joined:
    Aug 7, 2005
    Messages:
    2,183
    It is puzzling, but there is a very practical solution.

    PHP:
    if( $marblecount == )
    {
    $marblecount 1;
    }

    else 
    $marblecount++;
    ;)
     
  6. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    It is puzzling. Could it be something to do with the default value in the database? It's set as:
    INT(11) default 0
    though, so I can't see why that should cause a problem.
    Erik, I'm using a workaround similar to Eriks at the moment, but why is this happening?

    Jonathon: I've been doing that (see my previous post) and it shows 0 in the database using phpMyAdmin, but the SELECT query returns 1!
     
  7. JonathanAnon

    JonathanAnon Banned

    Joined:
    Dec 17, 2004
    Messages:
    531
    Yeah it is strange.
    But just take it step by step - When you have say id=50 and marblecount=0
    what happens when you just try to pull the marblecount for id 50, before you carry out any operations on it. If it is set as 0 in the DB and shows up as 1 in the PHP code then there must be something dodgy with the PHP function that you are using to access the DB.
     
  8. DrP

    DrP Thread Starter

    Joined:
    Jul 23, 2005
    Messages:
    739
    That's exactly what it is doing!
    $sql = mysql_query("SELECT marblecount FROM marbles_collection WHERE id = '$id'") or die(mysql_error());
    $row = mysql_fetch_array($sql);
    extract($row);
    echo $marblecount;

    This outputs a value of '1' even when the same value in the db is '0'! However, if the value in the database is 1 or more, then the above query returns the correct value!
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/522218