Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.