0

Okay I tried this before and it got closed because it wasn't specific. Below is my entire page code. Please ignore the font and html stuff because all of that is working fine. My Specific question is how to alter this so that the column called "Given" is reset to 0 (zero) at the first day of each month automatically. I am only specifically asking for how to make this happen.

Everything else is working to manually add and subtract, etc. I only need assistance in making that column reset to zero on the first day of each month.

TO BE EVEN MORE SPECIFIC the column "OWED" in the database should be reset to 0 (zero) at the first day of each month automatically to achieve this.

I am new to coding and this code was made for me months ago, and while I have learned a lot since then, I do not seem to find any example to learn from on how to make what I am asking happen. Thank you

EDIT - The objective is to reset all of the rows in the "Owed" column at the same time. Either automatically each month, or manually with one button (or image button)

<?php
$servername = "localhost";
$username = " ";
$password = " ";
$db=" ";
$conn = mysqli_connect($servername, $username, $password,$db);

// $sql = "INSERT INTO `number`( `number`) 
// VALUES ('$qty')";
$sql = "SELECT id,name,requests,owed FROM record ORDER BY name";

$result = $conn->query($sql);

?>

<!DOCTYPE html>
<html>
<head>
<style>
    table#customers {
    width: fit-content;
    margin: auto;
    margin-top: 2px;
}
#customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td, #customers th {
  border: 2px solid #B7B7B7;
  padding: 4px;
}

#customers tr:nth-child(even){background-color: #ffffff;}
#customers tr:nth-child(odd){background-color: #f2f2f2;}

#customers tr:hover {background-color: aliceblue;}

#customers th {
  padding-top: 2px;
  padding-bottom: 2px;
  text-align: center;
  background-color: #FFB951;
  color: black;
}
.sub-add{
    float: right;
}
span.sub-add a {
    text-decoration: none;
    margin: 1px;
}
</style>

</head>
<body leftmargin="0" topmargin="0">
    <meta http-equiv="refresh" content="10" >
    <table id="customers" bgcolor="#CCCCCC">
    <tr> 
        <th height="5" width="20%">Name</th>
        <th height="5" width="20%" style="display:none">Requests</th>
        <th height="5" width="10%">Earned</th>
        <th height="5" width="10%">Given</th>
        <th height="5" width="10%">Owed</th>
    </tr>
<?php 
if ($result->num_rows > 0) {
    // output data of each row

    while($row = $result->fetch_assoc()) {
        $earned = $row["requests"]/1;
        $owed = $row["owed"];
        $earned = (int)$earned;
        $owedtoo = $earned - $owed;
        echo  '<tr><td><center><b><font color="Orange">'.$row["name"].'<font></b><br><a onclick="return confirm(\'Are You SURE You Want To Delete?\')" href="delete.php?id='.$row['id'].'">Delete</a> | <a href="updateentry.php?id='.$row['id'].'">Edit</a>
</center></td><center><td style="display:none"><center>'.$row["requests"].'<span class="sub-add"><a href="update.php?id='.$row["id"].'&method=minus&update=request"><span>-</span></a>&nbsp<a href="updaterequests.php?id='.$row['id'].'"><span> + &nbsp</center></span></a></span></td></center><td><center><b><font color="green">'.$earned.'</font></b></td><center><td><center><b>'.$row["owed"].'</b><span class="sub-add"><a href="update.php?id='.$row["id"].'&method=minus&update=owed"><span> - </span></a>&nbsp<a href="update.php?id='.$row["id"].'&method=add&update=owed"><span>  + </span></a></span></center></td><td><center><b><font color="red">'.$owedtoo.'</font></b></td></tr>';

    }
} 
?> 
</table>

</body>
</html>
<?php $conn->close(); ?>

  • Ok, so the column called `Given` OR the column called `OWED` or maybe both of them? – RiggsFolly May 24 '22 at 16:32
  • @RiggsFolly yes, so the column called "Given" is pulling from the Mysql column called "Owed". SO I'm thinking if I can have an auto reset on the first day of each month to reset "Owed" to zero (0) then it will take care of it. If this isn't possible, then maybe a button to reset it I can do manually but auto is preferred – New Developer 2021 May 24 '22 at 16:35
  • Ok, this is not as simple a thing to do as you may initially think. 1. Is this page something that only you can run or is it a page run by normal users – RiggsFolly May 24 '22 at 16:38
  • 1
    You need a `cron-script` that runs every-day at midnight, preferably during off-peak hours to check/do your daily, weekly, monthly maintenance. – जलजनक May 24 '22 at 16:41
  • @जलजनक Well first of the month would be more on spec – RiggsFolly May 24 '22 at 16:42
  • This is just a page that me and some moderators run. A public view doesn't have the add or subtract option. If it's easier to do that with a button to manually do it each month that is fine too. I just don't want to manually have to reset 500+ rows manually each row every month. A function to do all rows in that column is what I'm looking for. (Maybe I should have said that in the question too) – New Developer 2021 May 24 '22 at 16:43
  • Research what a `cron` job is if you are using linux. Or if you are using windows a Scheduled Task. These can be set to run once at a specific time like the First of the month at `00:01:00` This removed a lot of the potential pitfalls of writing some PHP to do it that could be run more than once by more than 1 person by accident – RiggsFolly May 24 '22 at 16:46
  • Then write a simple PHP script to simply run the query to zeroise that column and get the CRON / Scheduled Task to run that for you on the first of the month – RiggsFolly May 24 '22 at 16:49
  • 1
    MySQL also has [Event Scheduler](https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html) support that can be used to schedule reoccurring queries. – Will B. May 24 '22 at 16:56
  • @RiggsFolly I made it work with an edit button then a separate php page that reset all of the rows in the column. Thanks for the help. – New Developer 2021 May 24 '22 at 18:21
  • `the column "OWED" in the database should be reset to 0 (zero) `...in every row? In that case: `UPDATE record SET owed = 0` will reset all of them – ADyson May 24 '22 at 18:44

1 Answers1

1

Use cron jobs for routine or date/time specific tasks.

Good question thread here: How to create cron job using PHP?

ld98
  • 120
  • 1
  • 9