Jump to content

Problem with adding 1 to a counter


xsubs

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/74645-problem-with-adding-1-to-a-counter/
Share on other sites

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.