首页 > 解决方案 > 在mysql和php中将数据从一个数据库移动到另一个数据库时出错

问题描述

在我的网站中,我有三个数据库:新任务、待处理任务和已完成任务。要将数据从新任务移动到待处理任务,但是当我尝试将数据从待处理任务移动到已完成任务时,我收到以下错误:

注意:未定义索引:第 66 行 C:\wamp64\www\Test\mark-complete.php 中的 id 调用堆栈 #TimeMemoryFunctionLocation 10.0008247288{main}( )...\mark-complete.php:0 "/>

第 66 行是以下代码:

<input type="hidden" name="id" value="<?php echo trim($_GET["id"]); ?>"/>

这是我将数据从待处理移动到完成的完整代码:

<?php
if(isset($_POST["id"]) && !empty($_POST["id"])){
require_once 'config.php';
$param_id = trim($_POST["id"]);

$sql1 = "INSERT INTO completetask SELECT * FROM pendingtask WHERE id = ? ";
$sql2 = "DELETE FROM pendingtask WHERE id = ? ";


if($stmt = mysqli_prepare($link, $sql1)){
    mysqli_stmt_bind_param($stmt, "i", $param_id);
    if(mysqli_stmt_execute($stmt)){
        // Record inserted successfully. Close statement and delete record 
 from table_1
        mysqli_stmt_close($stmt);
        if($stmt = mysqli_prepare($link, $sql2)){
            mysqli_stmt_bind_param($stmt, "i", $param_id);
            if(mysqli_stmt_execute($stmt)){
                // Close statement
                mysqli_stmt_close($stmt);
                // Records deleted successfully. Redirect to landing page
                header("location: notifications.php");
                exit();
            }else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
    } else{
        echo "Oops! Something went wrong. Please try again later.";
    }
}

// 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();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View Record</title>
<link href="assets/css/bootstrap.css" rel="stylesheet" />
<style type="text/css">
    .wrapper{
        width: 500px;
        margin: 0 auto;
    }
</style>
</head>
<body>
<div class="wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="page-header">
                    <h1>Move Task to Completed Tasks</h1>
                </div>
                <form action="<?php echo 
 htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                    <div class="alert alert-danger fade in">
                        <input type="hidden" name="id" value="<?php echo 
 trim($_GET["id"]); ?>"/>
                        <p>Are you sure you want to move to Completed Tasks? 
 </p><br>
                        <p>
                            <input type="submit" value="Yes" class="btn btn- 
 danger">
                            <a href="notifications.php" class="btn btn- 
 default">No</a>
                        </p>
                    </div>
                </form>
            </div>
        </div>        
    </div>
</div>
</body>
</html>

标签: phpmysql

解决方案


尝试用类似的东西替换那个错误的陈述:

<?php echo isset($_POST["id"]) ? $_POST["id"] : ""); ?>

$_GET["id"]除非您使用查询字符串请求,否则不会有任何内容,例如。/index.php?id=420.


推荐阅读