skip to Main Content

this is my program, I have tried but it inserts with duplicate data
please help me

$minute = date("i:s");
$datesql = date('Y-m-d H:i:s');
if($minute=="10:00"){
    $qrecord = mysqli_query($conn220,
        "INSERT INTO `history_rpm_mesin`(`id`, `nomesin`, `shift`, `nama_op`, `flv`, `rpm`, `std`, `time_stamp`, `dept`) 
        VALUES ('','$nomesin','$sh_op','$namaop','$flv','$value','$rpm_op','$datesql','BUMBU')");
}

2

Answers


  1. Yes it is possible. But you will need to look at something like cron to run your script at the scheduled time.

    This is the cron expression for running your script every 10 minutes:

    */10 * * * * php your-script.php
    

    For this to work though, your script needs to be callable via the PHP CLI. That means it will not be executing in the same context as a web request.

    Depending on what the rest of the code looks like, you may need to make some changes in your assumptions about where the data comes from.

    At the very least, when using cron in this way, the code you’ve shown could be simplified to:

    $qrecord = mysqli_query($conn220,
        "INSERT INTO `history_rpm_mesin`(`id`, `nomesin`, `shift`, `nama_op`, `flv`, `rpm`, `std`, `time_stamp`, `dept`) 
        VALUES ('','$nomesin','$sh_op','$namaop','$flv','$value','$rpm_op',date('Y-m-d H:i:s'),'BUMBU')");
    

    NB: I’m ignoring issues around where the variables are coming from, but you should be careful here. If these variables are from user input, this query is wide open to SQL-injection attacks.

    Login or Signup to reply.
  2. Use cronjob would be a better choice but if you want to run script inside a page, consider that you need to check if last 10 min is already exist or not. So your code will be something like this:

    <?php
    
    //based on your need, we check time every 10 minute
    $minute = round(date("i")/10);
    
    /*
        add a query to check if current 10 min exist
        if not exist {
    */
    
    $datesql = date('Y-m-d H:i:s');
    $qrecord = mysqli_query($conn220, "INSERT INTO `history_rpm_mesin`(`id`, `nomesin`, `shift`, `nama_op`, `flv`, `rpm`, `std`, `time_stamp`, `dept`) VALUES ('','$nomesin','$sh_op','$namaop','$flv','$value','$rpm_op','$datesql','BUMBU')");
    
    /*
        }
    */
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search