从数据库中删除记录并将其保存到文件 - PHP

Yes, as my question asks, I need to delete records and the same records that I deleted save them dynamically in a file

I can only view or delete them

<?php

    session_start();

    include 'connessione.php';

    $id = $_SESSION['id'];

    $query_string = "SELECT * FROM utenti ORDER BY id DESC LIMIT 50";
    $query = mysqli_query($connessione, $query_string);

    ?>
    <?php

    while($row = mysqli_fetch_assoc($query)){ ?>

    <?php echo $row['id'] ;?>

    <?php } ?>

understanding this process I am able to delete data in my db but keep a local copy just in case

You could save these results in a JSON array.

<?php

    //Start the session
    session_start();

    //Include connection
    include 'connessione.php';

    //Get user ID
    $id = $_SESSION['id'];

    //Query to get stuff from database
    $query_string = "SELECT * FROM utenti ORDER BY id DESC LIMIT 50";
    $query = mysqli_query($connessione, $query_string);

    //Get results
    $results = mysqli_fetch_assoc($query);

    //Make that into a JSON array
    $results = json_encode( $results );

    //Put those results in a file (create if file not exist)
    $fileName = 'backup_file_' . time() . '.txt';
    $file = fopen( $fileName , 'a'  );
    fwrite( $file, $results );
    fclose( $file );

    //Delete the rows that you just backed up
    $query_delete = "DELETE FROM utenti ORDER BY id DESC LIMIT 50";
    mysqli_query( $connessione, $query_delete );

?>

for example, if your table looks like this:

| id | name | email_address |
|----+------+---------------|
| 1  | dave | dave@test.com |
| 2  | emma | emma@test.com |
| 3  | mark | mark@test.com |
+----+------+---------------+

your backup file will look like this:

[{"id":1,"name":"dave","email_address":"dave@test.com"},{"id":2,"name":"emma","email_address":"emma@test.com"},{"id":3,"name":"mark","email_address":"mark@test.com"}]

Yes,you can save them in file before just delete records.