我的代码创建了一个备份文件,备份了数据库中存在的数据,经过7天后,我想知道(如果进行测试)不应删除的数据,可以说是两条记录,今天,它们没有被正确删除,但是无论如何都创建了备份文件,我不希望这样做,我希望仅在删除发生时才创建备份文件,然后尝试重命名备份文件,因为创建后,我希望将其与备份的日期一起保存,但是我跳过了所有内容,我不明白为什么
我只希望在数据库中有结果将要删除时创建备份文件。
我希望备份文件具有当前日期作为文件名。
<?php
//Start the session
session_start();
//Include connection
include 'connessione.php';
$query = $connessione->query("
SELECT *
FROM utenti
WHERE data_password < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))
");
$results = $query->fetch_all(MYSQLI_ASSOC);
$ok = json_encode( $results );
function makeDirectory($path, $mode)
{
$return = mkdir($path, $mode, true);
return $return === true || is_dir($path);
}
//Create the directory for backup files.
$path = 'backup_LOG';
$mode = 0777;
$risultato = makeDirectory($path, $mode);
//Put those results in a file (create if file not exist)
$fileName = 'backup_LOG/backup_file_' . time() . '.txt';
$file = fopen( $fileName , 'a' );
fwrite( $file, $ok );
fclose( $file );
if (count($results) > 0) {
// @todo backup the data
// delete fetched rows
$firstId = reset($results)['data_password'];
$lastId = end($results)['data_password'];
$stmt = $connessione->prepare("DELETE FROM utenti WHERE data_password < DATE_SUB(NOW() , INTERVAL 7 DAY)");
$stmt->bind_param('ii', $firstId, $lastId);
$stmt->execute();
}
?>