xsubs Posted October 24, 2007 Share Posted October 24, 2007 I needed a code to add one to a counter, so i built this: function FileWrite($sign, $act, $id) { include("./Configs/Config.php"); $Querygftext = mysql_query("SELECT * FROM Text WHERE id = '$id'"); while ($TextRow = mysql_fetch_array($Querygftext)) { if ($sign == "Plus") { $FileAdd = $TextRow[$act]+1; } elseif ($sign == "Minus") { $FileAdd = $TextRow[$act]-1; } } $Querygftotal = mysql_query("SELECT * FROM Total"); while($TotalRow = mysql_fetch_array($Querygftotal)) { if ($sign == "Plus") { $TotalAdd = $TotalRow[$act]+1; } elseif ($sign == "Minus") { $TotalAdd = $TotalRow[$act]-1; } } mysql_query("UPDATE Text SET $act = '$FileAdd' WHERE id = '$id'"); mysql_query("UPDATE Total SET $act = '$TotalAdd'"); } Now, it works fine, but sometimes it just drives crazy, and changes the counter to large numbers, 3000+ (when it's not really true) Why does that happen? What can i do to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/ Share on other sites More sharing options...
trq Posted October 24, 2007 Share Posted October 24, 2007 Your logic doesn't really make much sense so its hard to tell what exactly your trying to do (I assume this is why your having issues) but its more than likely being caused by your loops. You could however (if I understand whats meant to happen) make your code alot simpler. <?php function FileWrite($sign, $act, $id) { include_once "./Configs/Config.php"; $action = array('Plus' => '+','Minus' => '-'); if (!mysql_query("UPDATE Text SET $act = $act{$action[$sign]}1 WHERE id = '$id'")) { return false; } if (!mysql_query("UPDATE Total SET $act = $act{$action[$sign]}1")) { return false; } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/#findComment-377349 Share on other sites More sharing options...
xsubs Posted October 25, 2007 Author Share Posted October 25, 2007 Ok, it works, it adds one to the counter. Now i'll wait and see if drives crazy again and become 1231231. Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/#findComment-377624 Share on other sites More sharing options...
xsubs Posted October 28, 2007 Author Share Posted October 28, 2007 Nothing helps! I did what was told me, i tried everything! It goes CRAZY! Like here: Please, people, help me solve this, because i get crazy too because of that. Thank YOU! Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/#findComment-379919 Share on other sites More sharing options...
xsubs Posted October 29, 2007 Author Share Posted October 29, 2007 someone, please? Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/#findComment-380156 Share on other sites More sharing options...
LemonInflux Posted October 29, 2007 Share Posted October 29, 2007 SELECT *your query so you only get the thing you want to update $new_row = $row['your_row']++; UPDATE *your table* SET `your_counter_field` = $new_row; Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/#findComment-380175 Share on other sites More sharing options...
xsubs Posted October 29, 2007 Author Share Posted October 29, 2007 Whats the difference? Quote Link to comment https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/#findComment-380230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.