FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Odd PHP math...





Diablosblizz
I don't quite get this one, and I can't seem to figure it out. I'm using the code below:

Code:
$fetch1 = mysql_query("SELECT * FROM `forum_thread` WHERE `id` = '$_GET[id]'");
$thread = mysql_fetch_array($fetch1);
$views = $thread['views'];
$addview = $views + 1;
$updateview = mysql_query("UPDATE `forum_thread` SET `views` = '$addview' WHERE `id` = '$_GET[id]'");
echo $addview;


Basically, it selects the view row from the table in the database. It then adds one to it and updates the database. When I echo out the $addview variable it states the right number, but it's added to the database it's added twice.

Anybody have any ideas why this is adding twice when it's going into the database?
imagefree
Diablosblizz wrote:
I don't quite get this one, and I can't seem to figure it out. I'm using the code below:

Code:
$fetch1 = mysql_query("SELECT * FROM `forum_thread` WHERE `id` = '$_GET[id]'");
$thread = mysql_fetch_array($fetch1);
$views = $thread['views'];
$addview = $views + 1;
$updateview = mysql_query("UPDATE `forum_thread` SET `views` = '$addview' WHERE `id` = '$_GET[id]'");
echo $addview;


Basically, it selects the view row from the table in the database. It then adds one to it and updates the database. When I echo out the $addview variable it states the right number, but it's added to the database it's added twice.

Anybody have any ideas why this is adding twice when it's going into the database?


Added Twice means? Is it effecting 2 rows? if yes, then the problem may be in the `id` field. You have the only condition "WHERE `id` = 'something' and i think $_GET['id'] is repeated twice in database. To avoid this problem you can add LIMIT 1 at the end of Query.

Remember: when u use LIMIT 1, it will update just the first recors found. Second recors will never be updated. To update second record, either add some other identifyting condition, or use "ORDER BY DESC MIMIT 1".

If by Added Twice you means that it ads 2 rather than 1 to the existing views, then i think the script is executed twice. For example, if this piece of script is taken from inside of a function, function may be called twice, or the piece of script may be inside a loop running 2 times. etc
Diablosblizz
Sorry, I mean that it's adding 2 rather than 1. There is no code in the script, I remove the query it doesn't add anything.
kv
The code seems to be alright. However, check if there is a possibility of the same code called twice (copy paste error, double function call, etc).

Actually, the same result can be achieved in a simple manner.

Code:
mysql_query("UPDATE `forum_thread` SET `views` = `views`+1 WHERE `id` = '$_GET[id]'");


Easy Very Happy
rvec
I hope you're not using that in production.
Better use this:
Code:
$id  = (int) $_GET[id];
$fetch1 = mysql_query("SELECT * FROM forum_thread WHERE id = $id");
$thread = mysql_fetch_array($fetch1);
$views = $thread['views'];
$addview = $views + 1;
mysql_query("UPDATE forum_thread SET views =views+1 WHERE id = $id");
echo $addview;
Diablosblizz
That seems to have worked Rvec, I don't really know why cause it's doing the same thing outside of the query... Thanks!
rvec
I added the $id = (int) $_GET['id']; You should really use that because else users could put sql in that value to do stuff you don't want them to (especially in the update).
Related topics
[Scripts] PHP Clock
PHP OOP Tutorial
Wordpress Template...very odd problem
PHP Math
PHP Problem...
Rebuilt site using php, google seems confused
math equations
How tough is PHP?
counter help
PHP Member System (using FOPEN)..
PHP and cookies. Simple problem.
PHP Whos Online Code OR Page refresh..
AJAX + PHP, UPDATE mysql table nto working.
Alternating between even and odd in a while loop
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.