简体   繁体   中英

How do I update date without changing the older date value in php?

I'm creating a withdraw function where user can only withdraw their money in bulk. If they have their transaction in the same day. They can only withdraw it the day after today. Meaning that they cannot withdraw their money in the same day that they have make their transaction. But I got a problem when user wants to withdraw their money. The older date_claim got changed too.

---------------------
|id   |transac_date | total | status  | date_claim |
--------------------------------------------------
|abc1 | 2020-10-07  | 98   | claimed  | 2020-10-10 |
|abc1 | 2020-10-22  | 124  |unclaimed | null       |
|abc1 | 2020-10-24  | 145  |unclaimed | null       |

in this case user are trying to claim the transaction date on 2020-10-22


if(isset($_POST['withdraw']))
{
   $id= $_GET['id'];
   $sql="UPDATE tbl_kqd SET status ='claimed', date_claim = CURRENT_DATE()
        WHERE id = '$id' AND 
        DATE(transac_date) !=CURRENT_DATE() ";
   $sql_claim = mysqli_query($link, $sql);
   
   if (mysqli_affected_rows($link) > 0){
    echo "<script>alert('Successfully Claimed!')</script>";
    echo "<script>window.location = 'claim.php'</script>";  
   }
  else
    echo "<script>alert('Please try tomorrow!')</script>";
}


//the output


---------------------
|id   |transac_date | total | status  | date_claim |
--------------------------------------------------
|abc1 | 2020-10-07  | 98   | claimed  | 2020-10-24 |
|abc1 | 2020-10-22  | 124  | claimed  | 2020-10-24 |
|abc1 | 2020-10-24  | 145  |unclaimed | null       |

the query makes the older date changed too. I don't want it to change date that has been claimed. How do I fix this problem?

In database I declare the date_claim as date

That's happen because your Id is the same on the 3 rows.! make a unique id for every row will fix it.

---------------------
|id   |transac_date | total | status  | date_claim |
--------------------------------------------------
|abc1 | 2020-10-07  | 98   | claimed  | 2020-10-24 |
|abc2 | 2020-10-22  | 124  | claimed  | 2020-10-24 |
|abc3 | 2020-10-24  | 145  |unclaimed | null       |

I understand that you want to update the first "unclaimed" row for the given id and whose transac_date is different than today. Here is one way to phrase this:

update tbl_kqd 
set status ='claimed', date_claim = current_date()
where id = ? and transac_date <> current_date and status = 'unclaimed'
order by transac_date 
limit 1

MySQL supports order by in update statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM