4

For example, i have a data input program And I want to delete my data automatically after 1 day of this data I input. how I do that?
Someone can explain in code?

Create.php these values are sent to the server

<form action="" method="post" enctype="multipart/form-data">
    <input type="text" name="name_portofolio">
    <textarea name="info_portofolio"></textarea>
    <input type="file" accept="image/*"  name="picture_portofolio"> 
    <button type="submit" name="submit">Save</button>
</form>

function-add.php

<?php

function create_data($name_portofolio, $info_portofolio,
                  $picture_portofolio)
{
   global $connect;
   $name_portofolio = mysqli_real_escape_string($connect, $name_portofolio);
   $info_portofolio = mysqli_real_escape_string($connect, $info_portofolio);


   $filePath = "picture/".basename($picture_portofolio["name"]);
   move_uploaded_file($picture_portofolio["tmp_name"], $filePath);

   $query = "INSERT INTO portofolio 
         (name_portofolio, info_portofolio, picture_portofolio) 
          VALUES ('$name_portofolio', '$info_portofolio', '$filePath')";

   if(mysqli_query($connect, $query))
   {
      return true;
   }else{
      return false;
   }
  } // create_data

db.php

<?php
$host = "127.0.0.1";
$user = "root";
$password = "";
$db = "wherco";

// create connection
$connect = new mysqli($host, $user, $password, $db);

// check connection
if($connect->connect_error) {
    die("connection failed : " . $connect->connect_error);
} else {
    // echo "Successfully Connected";
}

?>

thanks .

9
  • 1
    use cron jobs May 30, 2017 at 4:59
  • What is cron jobs?
    – user7852624
    May 30, 2017 at 5:00
  • @wawanD What is cron job May 30, 2017 at 5:05
  • 1
    MySQL events work like charm for things such as this
    – Allen King
    May 30, 2017 at 6:00
  • 1
    It is possible to solve the question using only MySQL.
    – Alexander
    May 30, 2017 at 15:07

2 Answers 2

9

Try to use regular events. To get started, enable the Event Scheduler using

SET GLOBAL event_scheduler = ON;

After that you could crate event that will check rows creation time. For example

CREATE EVENT recycling ON SCHEDULE EVERY 1 HOUR ENABLE 
  DO 
  DELETE FROM MyTable WHERE `timestamp_column` < CURRENT_TIMESTAMP - INTERVAL 24 HOUR;

If there is no column with timestamp of a row creation in your table, then you can create trigger that will insert current timestamp and inserted row identificator to auxiliary table.

CREATE TRIGGER logCreator AFTER INSERT ON MainTable
  FOR EACH ROW 
  INSERT INTO LogTable (MainID, Created) VALUES(NEW.id, CURRENT_TIMESTAMP);

Then you can use this log to get keys of main table that was created before specific time.

delimiter |
CREATE EVENT cleaner ON SCHEDULE EVERY 1 HOUR ENABLE 
  DO 
  BEGIN
    DECLARE MaxTime TIMESTAMP;
    SET MaxTime = CURRENT_TIMESTAMP - INTERVAL 24 HOUR;
    DELETE FROM MainTable 
    WHERE id IN (SELECT MainID FROM LogTable WHERE Created < MaxTime);
    DELETE FROM LogTable WHERE LogTable.Created < MaxTime;
  END |
  delimiter ;
1
  • @wawanD, is it possible to get feedback?.. Did the proposed answer solve your question?
    – Alexander
    Dec 22, 2017 at 14:02
-1

In your table "portofolio" add column created_at(datetime). Then in Cron Job, check the current datetime exceeds created_at(datetime) with 24hours and delete the records by mysql query like

DELETE FROM portofolio WHERE created_at<=DATE_SUB(NOW(), INTERVAL 1 DAY)

And run the cron job file every minute

1
  • Since mysql is running anyway and the operation shall be performed inside mysql i would use mysql so to not rely on another service (cron). Why would you recommend to use cron?
    – surfmuggle
    Jul 2, 2021 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.