Jump to content

Recommended Posts

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

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/243293-php-date-function-help/
Share on other sites

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
}

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 ?

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.

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 ??

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?

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.

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.