我想在另一个表上备份已删除的记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想在另一个表上备份已删除的记录相关的知识,希望对你有一定的参考价值。

在这只是删除记录,但我想点击删除按钮后备份。此外,如果记录未插入备份表(备份失败意味着),则不应删除并提供错误消息。并且执行时间不应该花费更多时间 - 在phpmysql,XAMPP中使用

<?php

// connect to the database
include('connect-db.php');

// confirm that the 'id' variable has been set
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
    // get the 'id' variable from the URL
    $id = $_GET['id'];

    // delete record from database
    if ($stmt = $mysqli->prepare("DELETE FROM players WHERE id = ? LIMIT 1"))
    {
        $stmt->bind_param("i",$id);
        $stmt->execute();
        $stmt->close();
    }
    else
    {
        echo "ERROR: could not prepare SQL statement.";
    }
    $mysqli->close();

    // redirect user after delete is successful
    header("Location: view.php");
}
else
{
    // if the 'id' variable isn't set, redirect the user
    header("Location: view.php");
}

?>
答案

将player表的副本作为players_backup制作并将数据首先插入到备份表中,一旦插入后获得num行大于零,则意味着已插入数据。在此之后,您可以从播放器表中删除数据。简单。

我假设玩家和players_bkp有相同的模式。

if (isset($_GET['id']) && is_numeric($_GET['id']))
{
    // get the 'id' variable from the URL
    $id = $_GET['id'];
    $sql  = "insert into players_bkp select * FROM players WHERE id = ? LIMIT 1";
    $stmt = $mysqli->prepare($sql);
    $rc = $stmt->bind_param('i',$id);
    $rc = $stmt->execute();
    $tid = $stmt->insert_id;
    $stmt->close();
    if($tid  > 0){  //that means record is inserted and you can actually delete the record from your players table
        // delete record from database
        if ($stmt = $mysqli->prepare("DELETE FROM players WHERE id = ? LIMIT 1"))
        {
            $stmt->bind_param("i",$id);
            $stmt->execute();
            $stmt->close();
        }
        else
        {
            echo "ERROR: could not prepare SQL statement.";
        }
        $mysqli->close();

        // redirect user after delete is successful
        header("Location: view.php");
    }
}
else
// if the 'id' variable isn't set, redirect the user
{
    header("Location: view.php");
}

以上是关于我想在另一个表上备份已删除的记录的主要内容,如果未能解决你的问题,请参考以下文章

SSIS - 在另一个表上执行查找以获取相关列

我想在同一张表上创建更多触发器

是否可以在 hive 中已分区的表上添加分桶?

删除单个表时 SQL Server 触发器

回收站视图未显示在片段中

MS SqlServer 通过数据库日志文件找回已删除的记录