skwap Posted July 30, 2011 Share Posted July 30, 2011 Friends, I need a small help. I have a small website & their users can request payment , the users can only request 1 payment in a week. Currently iam storing users payment request into a database below the demo of that database. id amount type date uid 1 $50 paypal 2011/07/30 5 suppose the user (uid = 5) can only request again payment after 7 days which means 2011/08/06. I currenly wrote a code which works daily payment means it return error message if the user request more than one payment in a day. <?php $date = date("Y/m/d"); $sql = mysql_query("SELECT * FROM `payments` WHERE `id` = '$uid' , `date` = '$date'"); $check = mysql_num_rows($sql); if($check > "0") { echo "you can only request one payment per day." } ?> I want to change below code as week payment means please change below code as weekly payment request (A user can only request 1 payment in a week) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/ Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2011 Share Posted July 30, 2011 What is the data type of the `date` field? Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1249502 Share on other sites More sharing options...
skwap Posted July 30, 2011 Author Share Posted July 30, 2011 What is the data type of the `date` field? I used date data type for it Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1249506 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2011 Share Posted July 30, 2011 So in the database, the date is stored as 2011-05-31, correct? This should do the job for you. A COUNT() query is almost always a better option if all you need to do is determine the number of matching records returned by the query, BTW. $query = "SELECT COUNT(1) FROM `payments` WHERE id = $uid AND DATE_ADD(`date`, INTERVAL 1 WEEK) < CURDATE()"; $result = mysql_query( $query ); $array = mysql_fetch_row( $result ); if( $array[0] > 0 ) { // a record was returned from within the last week echo "You may only request one payment per week."; } else { // no record from within last week. // process the payment code } Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1249513 Share on other sites More sharing options...
skwap Posted July 31, 2011 Author Share Posted July 31, 2011 So in the database, the date is stored as 2011-05-31, correct? This should do the job for you. A COUNT() query is almost always a better option if all you need to do is determine the number of matching records returned by the query, BTW. $query = "SELECT COUNT(1) FROM `payments` WHERE id = $uid AND DATE_ADD(`date`, INTERVAL 1 WEEK) < CURDATE()"; $result = mysql_query( $query ); $array = mysql_fetch_row( $result ); if( $array[0] > 0 ) { // a record was returned from within the last week echo "You may only request one payment per week."; } else { // no record from within last week. // process the payment code } Thanks, its working... Could you please explain the code ? Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1249728 Share on other sites More sharing options...
Pikachu2000 Posted July 31, 2011 Share Posted July 31, 2011 Which part of it? The query itself? Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1249944 Share on other sites More sharing options...
skwap Posted August 1, 2011 Author Share Posted August 1, 2011 Which part of it? The query itself? ya its query. also its mysql_fetch_row retrun value Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1250029 Share on other sites More sharing options...
Pikachu2000 Posted August 1, 2011 Share Posted August 1, 2011 All it really does is count the records that are matched and return that number. mysql_fetch_row takes the result resource and puts the record in an enumerated array. Since there is only one value returned, it will be in index 0 of the array. Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1250032 Share on other sites More sharing options...
skwap Posted August 4, 2011 Author Share Posted August 4, 2011 the code is not working i tried to request payment but its not showing the error message "You may only request one payment per week." users can request unlimited payments there are no restrictions. Below the table which i used while testing & code is yours id amount type date uid 1 10.00 paypal 2011-08-04 2 2 50.00 paypal 2011-08-10 1 3 100.00 paypal 2011-08-10 1 Code (I used while testing it myself) - uid = '1'; $query = "SELECT COUNT(1) FROM `payments` WHERE uid = $uid AND DATE_ADD(`date`, INTERVAL 1 WEEK) < CURDATE()"; $result = mysql_query( $query ); $array = mysql_fetch_row( $result ); if( $array[0] > 0 ) { // a record was returned from within the last week echo "You may only request one payment per week."; } else { echo "Ok"; } What is the problem ?? Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1251979 Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2011 Share Posted August 4, 2011 I have a type in the query string, the < less than should be a > greater than. But now that I see some of the data, I'm wondering if I misunderstood what you're trying to do. Are you trying to check only against payments that have already been requested, or is this to allow scheduling of future payments as well? Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1252186 Share on other sites More sharing options...
skwap Posted August 5, 2011 Author Share Posted August 5, 2011 The above table is used to store the user's payment requests. My aim is that a user can only request one payment in a week. If the user trying to request one more payment in a week the code should return the error message. After 1 week he can also request again 2nd payment. I think you get what i say. Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1252287 Share on other sites More sharing options...
skwap Posted August 6, 2011 Author Share Posted August 6, 2011 i just changed the part of a code DATE_ADD(`date`, INTERVAL 1 WEEK) > CURDATE(). Its worked.... Quote Link to comment https://forums.phpfreaks.com/topic/243293-php-date-function-help/#findComment-1253245 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.