Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

PHP increment help


(!)

DrP's Avatar
DrP DrP is offline DrP has a Profile Picture
Computer Specs
Member with 734 posts.
THREAD STARTER
 
Join Date: Jul 2005
Location: Norwich, UK
28-Nov-2006, 04:45 PM #1
PHP increment help
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!).

Last edited by DrP; 28-Nov-2006 at 04:52 PM..
JonathanAnon's Avatar
JonathanAnon JonathanAnon is offline
Account Disabled with 531 posts.
 
Join Date: Dec 2004
Experience: Advanced
29-Nov-2006, 12:52 PM #2
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.
DrP's Avatar
DrP DrP is offline DrP has a Profile Picture
Computer Specs
Member with 734 posts.
THREAD STARTER
 
Join Date: Jul 2005
Location: Norwich, UK
29-Nov-2006, 01:58 PM #3
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.
JonathanAnon's Avatar
JonathanAnon JonathanAnon is offline
Account Disabled with 531 posts.
 
Join Date: Dec 2004
Experience: Advanced
30-Nov-2006, 03:04 PM #4
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.
Eriksrocks's Avatar
Computer Specs
Member with 2,183 posts.
 
Join Date: Aug 2005
Location: Minnesota
Experience: Advanced
30-Nov-2006, 08:46 PM #5
It is puzzling, but there is a very practical solution.

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

else 
$marblecount++; 
DrP's Avatar
DrP DrP is offline DrP has a Profile Picture
Computer Specs
Member with 734 posts.
THREAD STARTER
 
Join Date: Jul 2005
Location: Norwich, UK
01-Dec-2006, 02:41 PM #6
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!
JonathanAnon's Avatar
JonathanAnon JonathanAnon is offline
Account Disabled with 531 posts.
 
Join Date: Dec 2004
Experience: Advanced
01-Dec-2006, 04:49 PM #7
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.
DrP's Avatar
DrP DrP is offline DrP has a Profile Picture
Computer Specs
Member with 734 posts.
THREAD STARTER
 
Join Date: Jul 2005
Location: Norwich, UK
02-Dec-2006, 07:21 AM #8
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!
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑