dsyq40772 2018-08-12 11:16
浏览 39

不从MySQL中的表中删除数据[重复]

This question is an exact duplicate of:

I am having an issue where I am trying to move data from table 1 to table 2 and then delete that row moved from table 1.

Here is my code:

<?php
if(isset($_POST["id"]) && !empty($_POST["id"])){

require_once 'config.php';


$sql = "INSERT INTO table_2 SELECT * FROM table_1 WHERE id = ? ";
$sql = "DELETE FROM table_1 WHERE id = ? ";


if($stmt = mysqli_prepare($link, $sql)){
    // Bind variables to the prepared statement as parameters
    mysqli_stmt_bind_param($stmt, "i", $param_id);

    // Set parameters
    $param_id = trim($_POST["id"]);

    // Attempt to execute the prepared statement
    if(mysqli_stmt_execute($stmt)){
        // Records deleted successfully. Redirect to landing page
        header("location: index.php");
        exit();
    } else{
        echo "Oops! Something went wrong. Please try again later.";
    }
}

// Close statement
mysqli_stmt_close($stmt);

// Close connection
mysqli_close($link);
} else{
// Check existence of id parameter
if(empty(trim($_GET["id"]))){
    // URL doesn't contain id parameter. Redirect to error page
    header("location: error.php");
    exit();
}
}
?>

The problem with the above code is that it deletes it from both tables. When I remove the $sql = "DELETE FROM table_1 WHERE id = ? "; from the code it then moves the data fine to table 2 but not deleting it from table1

</div>
  • 写回答

3条回答 默认 最新

  • dongran1779 2018-08-12 11:38
    关注

    You are overwriting your insert with delete ... for avoid this you could use different vars for query and execute the two different query ..

    $sql1 = "INSERT INTO table_2 SELECT * FROM table_1 WHERE id = ? ";
    $sql2 = "DELETE FROM table_1 WHERE id = ? ";
    
    
    if($stmt = mysqli_prepare($link, $sql1)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);
    
        // Set parameters
        $param_id = trim($_POST["id"]);
    
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records deleted successfully. Redirect to landing page
            header("location: index.php");
            exit();
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
    
    if($stmt = mysqli_prepare($link, $sql2)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "i", $param_id);
    
        // Set parameters
        $param_id = trim($_POST["id"]);
    
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            // Records deleted successfully. Redirect to landing page
            header("location: index.php");
            exit();
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么