Jump to content


Photo

script not functioning


  • Please log in to reply
9 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 01:09 PM

well actually it is functioning exactly like I told it, but it's not what I wanted.  Ok I did paypay integration,there is a time period on the database that is set to the number of days they paid for 30 or 365 which is 5.99 or 19.99.
everything works on the site now, I test everything as I do it, I was trying to get this to work, and it looked perfect, but when I retested it, I found out it's not functioning like I envisioned.

<?php
if($_SESSION['controller'] === true) { // updates paid time period
	$select = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]';";
	$query = mysql_query($select);
	$row = mysql_fetch_array($query);
	if ($row['timeperiod'] != "none") {
	$currentdate = date("m/d/y");
	$paydate = $row['paydate'];
	$currentdate = strtotime($currentdate);
	$paydate = strtotime($paydate);
$number_days = floor(($currentdate - $paydate)/86400); // 86400 = seconds per day
	$updatedversion = $row['timeperiod'] - $number_days;
	$update = "UPDATE userinfo SET timeperiod = '$updatedversion' WHERE username = '$_SESSION[username]';";
	$query2 = mysql_query($update);
	} // Closes check for timeperiod equaling none.
	$selectinfo = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]' AND timeperiod <= 0;";
	$query3 = mysql_query($selectinfo);
	if ($rowforupdate = mysql_fetch_array($query3)) {
	$paid = "no";
	$paypalid = "not applicable";
	$paydate = "not applicable";
	$timeperiod = "none";
	$updatepayinfo = "UPDATE userinfo SET paid = '$paid', paypalid = '$paypalid', paydate = '$paydate', timeperiod = '$timeperiod' WHERE id = '$_SESSION[id]';";
	$query4 = mysql_query($updatepayinfo);
	}// closes row to update payement information when number is 0 or below
}// closes test to update paid
}// close isset

?>
1. I test to see if my session is active, this is hte basis for all of this script to run.
2. It queries the database for information so I can use it in my script.  It calls the information using the user id number so there is no chance of error, I don't allow the same username in the database, but it's a safety precaution.
3. I check to see if timeperiod in the database is not set to none, if it's not then I run through an update script.
4. I pull all the date information together, and calculate it.( the intention is, I pull the date from the database, which is the date they paid, then I pull the current date, which is whatever today is, I then calculate the amount of days that passed in those time period, the timeperiod in the database is the days left on the active paid account.  After I calculate it, I subtract the new update, to the timeperiod, giving me the new appropriate sum.  and I look to see what itme period has, if it is less than or equal to 0 then I know the paid account is invalid, and do the proper actions to be able to change all teh information on the account to show there payment has been finished.  This also opens up the option for them to buy again on another page.
This is what I want to happen, but I didn't take this into account until it was too late, when someone log's in, it runs this script, if they log in again it runs it again, what can I do "if anything" to modify this script to update it properly, but not reupdate it every time they login, this doubles it.  If it has been 3 days, The only thing that crossed my mind, I am going to try now, but I still wanted advice in case it doesn't work, the last idea I had was do a quick test in the middle of the script, to see if the updated number is already finished, but I won't know how to integrate that in there anyway.  Any advice??
5. I do the database update to update the time period.
6. then I query the db again to make sure I have all updated information from my query,

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 01:19 PM

<?php
if($_SESSION['controller'] === true) { // updates paid time period
	$select = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]';";
	$query = mysql_query($select);// query for info
	$row = mysql_fetch_array($query); // fetch array
	if ($row['timeperiod'] != "none") { // check status of timeperiod
	$currentdate = date("m/d/y");// get's todays date
	$paydate = $row['paydate']; // get's db pay date
	$currentdate = strtotime($currentdate); // changes to time stamp
	$paydate = strtotime($paydate);
$number_days = floor(($currentdate - $paydate)/86400); // 86400 = seconds per day
	$updatedversion = $row['timeperiod'] - $number_days; // new time period
	$checkforupdate = "SELECT timeperiod FROM userinfo WHERE id = '$SESSION[id]' AND timeperiod != '$updatedversion';";
	$checkquery = mysql_query($checkforupdate);
	if ($checkrow = mysql_fetch_array($checkquery)) {
	$update = "UPDATE userinfo SET timeperiod = '$updatedversion' WHERE username = '$_SESSION[username]';";
	$query2 = mysql_query($update);
		}
	} // Closes check for timeperiod equaling none.
	$selectinfo = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]' AND timeperiod <= 0;";
	$query3 = mysql_query($selectinfo);
	if ($rowforupdate = mysql_fetch_array($query3)) {
	$paid = "no";
	$paypalid = "not applicable";
	$paydate = "not applicable";
	$timeperiod = "none";
	$updatepayinfo = "UPDATE userinfo SET paid = '$paid', paypalid = '$paypalid', paydate = '$paydate', timeperiod = '$timeperiod' WHERE id = '$_SESSION[id]';";
	$query4 = mysql_query($updatepayinfo);
	}// closes row to update payement information when number is 0 or below
}// closes test to update paid
?>
I may not be a grand master yet, but I am getting a hell of a lot better, I figured it out, but thanks for hte help, I ran a check before updating to see if the current update had already been made, i have to think for a minute to figure out if checking another day is going to mess it up

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 01:28 PM

I think I uncovered another issue.
How am I going to check for it each day, because again, if today it checks, then checks again today, it won't update.
But if tomorrow for instance, it will check again, and update more than necessary.
I may have to factor in another date, that shows last time updated, or something?  hmm, I could update the current date, hmm
any advice???

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 01:53 PM

I guess now I actually do need help, I worked on the script some more, I supposedly had it set up right, most of the functionality is there, but there are some quirks, I play around with one of the dates, to see what will happen when it updates tomorrow or the next day and it's acting up a little, it'll either throw the time period somewhere up in the thousands, or it will start increasing it by just a few.  Can someone help me understand what is causing this.
<?php
if($_SESSION['controller'] === true) { // updates paid time period
	$select = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]';";
	$query = mysql_query($select);// query for info
	$row = mysql_fetch_array($query); // fetch array
	if ($row['timeperiod'] != "none") { // check status of timeperiod
	$currentdate = date("m/d/y");// get's todays date
	$paydate = $row['paydate']; // get's db pay date
	$currentdate = "07/29/06"; //strtotime($currentdate); // changes to time stamp
	$paydate = strtotime($paydate);
	$datetoupdate = date("m/d/y");
$number_days = floor(($currentdate - $paydate)/86400); // 86400 = seconds per day
	$updatedversion = $row['timeperiod'] - $number_days; // new time period
	$checkforupdate = "SELECT timeperiod FROM userinfo WHERE id = '$_SESSION[id]' AND timeperiod != '$updatedversion';";
	$checkquery = mysql_query($checkforupdate);
	if ($checkrow = mysql_fetch_array($checkquery)) {
	$update = "UPDATE userinfo SET timeperiod = '$updatedversion', paydate = '$datetoupdate' WHERE username = '$_SESSION[username]';";
	$query2 = mysql_query($update);
		}
	} // Closes check for timeperiod equaling none.
	$selectinfo = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]' AND timeperiod <= '0';";
	$query3 = mysql_query($selectinfo);
	if ($rowforupdate = mysql_fetch_array($query3)) {
	$paid = "no";
	$paypalid = "not applicable";
	$paydate = "not applicable";
	$timeperiod = "none";
	$updatepayinfo = "UPDATE userinfo SET paid = '$paid', paypalid = '$paypalid', paydate = '$paydate', timeperiod = '$timeperiod' WHERE id = '$_SESSION[id]';";
	$query4 = mysql_query($updatepayinfo);
	}// closes row to update payement information when number is 0 or below
}// closes test to update paid
?>

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 02:01 PM

I also added in some functionality, I started updating the paydate, to the current date, and left a note for the user's that this is showing when the timeperiod was last updates, then it started doing those errors when I started commenting out the current date, and adding a date that was a few days in advance??

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 28 July 2006 - 03:36 PM

strtotime() has issues with certian date formats, so you're probably getting invalid timestamps when you pass the function a date in ("m/d/y") format. According to the GNU Dates Input Format, one of the best recommeded formats is ("Y-m-d"). Ensure that the paydate value in your database is in this same format, so you will be comparing timestamps correctly.

Also, there should be no need for you to update the paydate field in your database when you are simply checking is there are days left in timeperiod. Also, this will cause you to have incorrect information on when a user last paid, so I would only update this value when a user pays for an extended time period, or their timeperiod expires.

Lastly, you are making up to 5 database calls (3 "SELECT", 2 "UPDATE") where you only need 2 (One "SELECT", One "UPDATE"). Here is some revised code including my two recommendations:

if($_SESSION['controller'] === true) { // updates paid time period
	$select = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]';";
	$query = mysql_query($select);// query for info
	$row = mysql_fetch_array($query); // fetch array

	if ($row['timeperiod'] != "none") { // check status of timeperiod
		$currentdate = strtotime(date("Y-m-d"); // get todays date, changed to timestamp
		$paydate = strtotime($row['paydate']); // change paydate to timestamp
		$number_days = floor(($currentdate - $paydate)/86400); // 86400 = seconds per day
		$updatedversion = $row['timeperiod'] - $number_days; // new time period

		if ($row['timeperiod'] != $updatedversion) {
			if ($updatedversion <= 0) {
				$paid = "no";
				$paypalid = "not applicable";
				$paydate = "not applicable";
				$timeperiod = "none";
				$updatepayinfo = "UPDATE userinfo SET paid = '$paid', paypalid = '$paypalid', paydate = '$paydate', timeperiod = '$timeperiod' WHERE id = '$_SESSION[id]';";
				$query4 = mysql_query($updatepayinfo);
			} else {
				$update = "UPDATE userinfo SET timeperiod = '$updatedversion' WHERE username = '$_SESSION[username]';";
				$query2 = mysql_query($update);
			}
		}
	}
}

Let me know how this works out for you.

#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 03:48 PM

I tried it, but it gave me the exact same issue I had before with my original script with every login, it doubles hte results, but I redid my script and everything works perfect, just with a lot of db calls I might try to fix some of the db calls, and clean it up some, but everything functions perfectly.  I haven't tested it once it goes negative but I am about to.
<?php
// start
	if($_SESSION['controller'] === true) { // updates paid time period
	$select = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]';";
	$query = mysql_query($select);// query for info
	$row = mysql_fetch_array($query); // fetch array
	if ($row['timeperiod'] != "none") { // check status of timeperiod
	$currentdate = "08/23/06";  //date("m/d/y");// get's todays date
	$paydate = $row['paydate']; // get's db pay date
	$currentdate = strtotime($currentdate); // test later with later date
	$paydate = strtotime($paydate);
	$datetoupdate = "08/23/06"; //date("m/d/y");
$number_days = floor(($currentdate - $paydate)/86400); // 86400 = seconds per day
	$updatedversion = $row['timeperiod'] - $number_days; // new time period
	$checkforupdate = "SELECT timeperiod FROM userinfo WHERE id = '$_SESSION[id]' AND timeperiod != '$updatedversion';";
	$checkquery = mysql_query($checkforupdate);
	if ($checkrow = mysql_fetch_array($checkquery)) {
	$update = "UPDATE userinfo SET timeperiod = '$updatedversion', paydate = '$datetoupdate' WHERE username = '$_SESSION[username]';";
	$query2 = mysql_query($update);
		}
	} // Closes check for timeperiod equaling none.
	$selectinfo = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]' AND timeperiod <= '0';";
	$query3 = mysql_query($selectinfo);
	if ($rowforupdate = mysql_fetch_array($query3)) {
	$paid = "no";
	$paypalid = "not applicable";
	$paydate = "not applicable";
	$timeperiod = "none";
	$updatepayinfo = "UPDATE userinfo SET paid = '$paid', paypalid = '$paypalid', paydate = '$paydate', timeperiod = '$timeperiod' WHERE id = '$_SESSION[id]';";
	$query4 = mysql_query($updatepayinfo);
	}// closes row to update payement information when number is 0 or below
}// closes test to update paid
?>
if you can help me fix some of those db calls now without breaking the functionality of the script it would help a lot.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 04:12 PM

see what started happening is it started running through the script every time it entered, the reason for extra db calls was to retrieve the updated version of the information, because I had to test the status on some fields, do a few updates based on that, then test it again, do some more updates then finally, test for 1 more thing, which included some previous updates in the same script, then run a few more queries.  I got everything functioning currently, it all functions like I want, so I am not being picky, If I can get it optimized, or cut down to less lines of code, to achieve the same successful result, then I don't mind giving it a try I can save my code on another page, and just pull it back if something goes wrong.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#9 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 28 July 2006 - 05:02 PM

I'm glad you figured out your issue.

I was mentioning lowering your number of DB calls, as you seem to be updating user information with an updated value, then pulling that exact same value from the DB 1 line of code later, when you had the updated version of the value already set in a variable. I just don't see the need to check the status when you have a variable that tells you the status 1 line of code earlier. Not much could have changed in that fraction of a second.

Anyway, if you want to try it, this should get rid of some of your db calls:

<?php
// start
if($_SESSION['controller'] === true) { // updates paid time period
	$select = "SELECT * FROM userinfo WHERE id = '$_SESSION[id]';";
	$query = mysql_query($select);// query for info
	$row = mysql_fetch_array($query); // fetch array
	
	if ($row['timeperiod'] != "none") { // check status of timeperiod
		$currentdate = "08/23/06";  //date("m/d/y");// get's todays date
		$paydate = $row['paydate']; // get's db pay date
		$currentdate = strtotime($currentdate); // test later with later date
		$paydate = strtotime($paydate);
		$datetoupdate = "08/23/06"; //date("m/d/y");
		$number_days = floor(($currentdate - $paydate)/86400); // 86400 = seconds per day
		$updatedversion = $row['timeperiod'] - $number_days; // new time period

		if($row['timeperiod'] != $updatedversion){
			if($updatedversion <= 0){
				$paid = "no";
				$paypalid = "not applicable";
				$paydate = "not applicable";
				$timeperiod = "none";
				$updatepayinfo = "UPDATE userinfo SET paid = '$paid', paypalid = '$paypalid', paydate = '$paydate', timeperiod = '$timeperiod' WHERE id = '$_SESSION[id]';";
				$query4 = mysql_query($updatepayinfo);
			} else {
				$update = "UPDATE userinfo SET timeperiod = '$updatedversion', paydate = '$datetoupdate' WHERE username = '$_SESSION[username]';";
				$query2 = mysql_query($update);
			}
		}
		
	} // Closes check for timeperiod equaling none.
}// closes test to update paid
?>



#10 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 05:43 PM

I will try this a little later thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users