简体   繁体   中英

MySQL table not updating through PHP cron job at specified time

I am working on an Android App which has a MySQL database which contains a table that has all the login details of each user.In that table I have column named "Status" which is initially assigned to 0 for every user.Everyday when the user logs in to his account and submits the data(clicks the submit button) the status is changed to 1.And every night at 12 AM the status should be reset to 0.Also, if the user doesnot submit the data until 10 AM for a day, a row should be automatically inserted to the MySQL table with a column value as "No Records" for the particular user whose status is still 0.So,I have a created a PHP file which runs as a cron job in cpanel. The PHP file checks the time every hour. And the current time 12 AM and 10 AM, it should do the required changes.But I am not getting the required result with my PHP script.The script is given below:

<?php

require "connection.php";


$get_time = "select date_format(NOW(), '%H:%i A') as current_daily_date from dual;";
$convert_time = mysqli_query($connect,$get_time);
$php_time = mysqli_fetch_assoc($convert_time);
$final_time = $php_time["current_daily_date"];


$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata')); 
$d = $dateTime->format("H:i A ");
$e = $dateTime->format("d/m/y"); 
echo $d;

$s = '00:00';
$g = strtotime($s);
$p = date('H:i A',$g);
echo $p;

$c = '10:00';
$q = strtotime($c);
$r = date('H:i A',$q);
echo $r;

if($d == $p) {

        $run = "UPDATE login SET status = 0;";
        $connect->query($run);  
                echo "Status updated";

   }

else if($d == $r) {

     $second_run = "INSERT into attendance (name,date,attendance) VALUES ((SELECT name from login where status = 0 ), '$e' , 'No Records');";
     $connect->query($second_run);  
         echo "Marked as No Records";

 }

 else {

     echo "Not the correct time to change the status.";
 }

 $connect->close();

 ?>

I have set my email for the cron job so that I get the output to my mail. But even at 12 AM ,the table did not update.

The output was:

00:00 AM 00:00 AM10:00 AMNot the correct time to change the status.

I don't know where I am going wrong.Can anyone please check and let me know what is the issue?

The format of the INSERT SELECT is incorrect:

$second_run = "INSERT into attendance (name,date,attendance) SELECT name, ****, **** from login where status = 0";

You don't need the VALUES bit and I'm not sure what the end segment was at all. You also are trying to insert 3 values whilst only providing one, I've put ****'s in there where you need to put your other values.

You should also turn on error reporting in your script if you didn't get any errors from it as this would have been an error.

error_reporting(E_ALL);
ini_set('display_errors', 1);

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