5 Replies - 293 Views - Last Post: 24 September 2017 - 10:30 PM Rate Topic: -----

#1 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

All Users Being Updated....Shouldn't be

Posted 24 September 2017 - 09:21 PM

I have been slowly figuring out problems and nailing out the bugs. Only problem this time around is that ALL of my users are being updated and it doesn't make sense to me as my query has a WHERE clause on it and the variable I use in it doesn't change unless done so through the registration form.

if(is_null($_SESSION["Registrationerrors"]))
	{
		$hashedPass = password_hash($_POST["pass"], PASSWORD_DEFAULT);
		try
		{
			$userID = insertUser(con(), $hashedPass, insertAddress(con()));
			if (is_null($userID) || $userID <= 0)
			{
				$_SESSION["Registrationerrors"] = "Error in saving user information.";
				redirectToRegisterPage();
			}
			if ((!isset($_POST["rfName"]) && !isset($_POST["rlName"]) && !isset($_POST["rUser"])) || (empty($_POST["rfName"]) && empty($_POST["rlName"]) && empty($_POST["rUser"])) ||
				(is_null($_POST["rfName"]) && is_null($_POST["rlName"]) && is_null($_POST["rUser"])))
			{
				redirectToLoginPage();
			}
			else
			{
				if (is_null($_POST["rfName"]) || empty($_POST["rfName"]))
				{
					$_SESSION["Registrationerrors"] = "You didn't enter your referree's first name.<br/>";
					delUserInfo(con(), $userID);
					redirectToRegisterPage();
				}
				if (is_null($_POST["rlName"]) || empty($_POST["rlName"]))
				{
					$_SESSION["Registionerrors"] = "You didn't enter your referree's last name.<br/>";
					delUserInfo(con(), $userID);
					redirectToRegisterPage();
				}
				if (is_null($_POST["rUser"]) || empty($_POST["rUser"]))
				{
					$_SESSION["Registionerrors"] = "You didn't enter your referree's BBTC username.";
					delUserInfo(con(), $userID);
					redirectToRegisterPage();
				}
				referral($_POST["rfName"], $_POST["rlName"], $_POST["rUser"], $userID);
			}
		}
		catch(PDOException $ex)
		{
			$_SESSSION["Registrationerrors"] = "Unknown error occurred.";
			redirectToRegisterPage();
		}
	}
	else
	{
		redirectToRegisterPage();
	}

All of it works here! It does what it is supposed to but the problem lies within the referral() method.

function referral($refFirst, $refLast, $refUser, $userID)
	{
		//Get Referree ID, Registration Date, Subscription End Date
		getReferreeInfo($refFirst, $refLast, $refUser);
		
		//Get Accounts 4 months old or less
		$getAccount = con() -> prepare("CALL get_less_than_four_month_accounts(:id)");
		$getAccount -> bindParam(":id", $_SESSION["RefID"]);
		$getAccount -> execute();
		$result = $getAccount -> fetchAll();
		foreach ($result as $row);
		{
			//If count of rows is greater than 0, and referral count for the month is greater than 2, update referree account
			if (count($row) > 0 && getReferralCount(con(), $_SESSION["RefID"]) < 2) { updateReferreeSubscription(con(), $_SESSION["RefID"], $_SESSION["SubEndDT"]); }
		}
	}


As you can see, I have an id for the Referree's ID (person who referred user signing up to site) for 4 month old or younger accounts. This should only return 1 row because the ID is unique per user (not null, primary, auto-incrementing field). Also, I look through the results to if there is any rows with this user (i'll change this up later to check the results before looping; just trying to get it to work right first). If the referree has less than 2 referrals for the month, then if should update that user only. Only problem is it updates all of them.

Picture 1 (What it's supposed to do, works hard coded in query)

Picture 2 (What the code does)

EDIT:

Pictures aren't uploading. In essence, one row is supposed to have 2 columns updated but it does every user in the table.

This post has been edited by dak45309: 24 September 2017 - 09:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: All Users Being Updated....Shouldn't be

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,842
  • Joined: 12-June 08

Re: All Users Being Updated....Shouldn't be

Posted 24 September 2017 - 09:49 PM

What does your update look like?
Was This Post Helpful? 0
  • +
  • -

#3 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: All Users Being Updated....Shouldn't be

Posted 24 September 2017 - 09:51 PM

View Postmodi123_1, on 25 September 2017 - 04:49 AM, said:

What does your update look like?


SP:
variables - UserID int, SubDT DATE
UPDATE `users` SET `SubscriptionendDT` = DATE_ADD(SubDT, INTERVAL 2 WEEK), `BasicPremium` = 1 WHERE `UserID` = UserID

This post has been edited by dak45309: 24 September 2017 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,842
  • Joined: 12-June 08

Re: All Users Being Updated....Shouldn't be

Posted 24 September 2017 - 09:57 PM

You are certain that is only being called once?
That there is only one ID being returned and used?
That the rows all don't have the same id?
Was This Post Helpful? 0
  • +
  • -

#5 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: All Users Being Updated....Shouldn't be

Posted 24 September 2017 - 10:12 PM

View Postmodi123_1, on 25 September 2017 - 04:57 AM, said:

You are certain that is only being called once?
That there is only one ID being returned and used?
That the rows all don't have the same id?


I var_dumped the $results for finding out if the account is less than or equal to 4 months old and it returns everything ignoring the where clause but the thing is that I pass in the referree's ID only and subscription end date to update them as needed and it doesn't change. I assigned the referree's information to session variables and null out the session variables every time the register page sends new information to the process page. Only problem is that it's updating every user.

updateReferreeSubscription(con(), $_SESSION["RefID"], $_SESSION["SubEndDT"]);


These are assigned when i get the information and never change from that point forward

function getReferreeInfo($first, $last, $user)
	{
		$getRefInfo = con() -> prepare("CALL get_referree_info(:user, :first, :last)");
		$getRefInfo -> bindParam(":user", $user);
		$getRefInfo -> bindParam(":first", $first);
		$getRefInfo -> bindParam(":last", $last);
		$getRefInfo -> execute();
		$result = $getRefInfo -> fetchAll(PDO::FETCH_ASSOC);
		foreach($result as $row)
		{
			$_SESSION["RefID"] = $row["UserID"];
			$_SESSION["RegistrationDate"] = $row["RegistrationDate"];
			$_SESSION["SubEndDT"] = $row["SubscriptionendDT"];
		}
	}

This post has been edited by dak45309: 24 September 2017 - 10:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: All Users Being Updated....Shouldn't be

Posted 24 September 2017 - 10:30 PM

Not sure what was going on but it was to do with the foreach loop...I got it fixed :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1