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

问题描述 投票:-1回答:1

在这只是删除记录,但我想点击删除按钮后备份。此外,如果记录未插入备份表(备份失败意味着),则不应删除并提供错误消息。并且执行时间不应该花费更多时间 - 在PHP,MYSQL,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");
}

?>
php mysql xampp
1个回答
1
投票

将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");
}
© www.soinside.com 2019 - 2024. All rights reserved.