PHP/MySQL:成功更新数据后显示一些错误 [英] PHP / MySQL: Display some error after successfully updated the data
问题描述
目前,我使用 CRUD 功能创建了一个系统.我的更新功能有一些问题.问题是,它会在 view_task.php 中显示错误,如:
Currently, I create a system using CRUD function. I have some problems with my update function. The problem is, it will display the error at view_task.php like:
1) 未定义索引:第 7 行的 report_id2) 未定义变量:第 50 行的 task_name
1) Undefined index: report_id at line 7 2) Undefined variable: task_name at line 50
问题是,当我检查数据库时,数据已经更新.
The problem is, when I check the database, the data has been updated.
以下是我当前的代码:
dashboard.php
dashboard.php
echo "<form method = 'post' action = 'view_task/view_task.php'>";
echo "<input type = 'hidden' name = 'report_id' value = '".$report_id."'>";
echo "<button type = 'submit' class='btn-primary'>View</button>";
echo "</form>";
view_task.php
view_task.php
<?php
require_once "../../../../config/configPDO.php";
require_once "../../../../config/check.php";
$report_id = $_POST['report_id']; //line 7
$sql = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid LEFT JOIN ot_team ON ot_team.team_id = ot_users.team_id WHERE report_id = :report_id";
$query = $conn->prepare($sql);
$query->execute(array(':report_id' => $report_id));
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$report_id = $row["report_id"];
$task_name = $row["task_name"];
}
?>
<form action="update_task_name.php" method="POST">
<td><b>Task Name</b></td>
<td colspan = '2'><input type="text" class="form-control" name="task_name" value="<?php echo $task_name; ?>"/></td> //line 50
<input type="hidden" name="report_id" value="<?php echo $report_id ?>">
<td><input type="submit" class="btn btn-primary btn-block" value = "Save" onclick="confirm('Are you sure?')"></td>
</form>
update_task_name.php
update_task_name.php
<?php
require_once '../../../../config/configPDO.php';
$update = "UPDATE ot_report SET task_name = :task_name WHERE report_id = :report_id";
$stmt = $conn->prepare($update);
$stmt->bindParam(':task_name', $_POST['task_name']);
$stmt->bindParam(':report_id', $_POST['report_id']);
$stmt->execute();
class Result {}
$response = new Result();
$response->result = 'OK';
$response->message = 'Update successful';
header("Location: view_task.php");
?>
谁能帮我解决这个问题?谢谢
Can anyone help me to fix this problem? Thanks
推荐答案
在 view_task.php 页面中,数据更新后没有定义 $_POST ['report_id'] 部分,因为在 update_task_name.php 中您使用了重定向标头("Location: view_task.php") 成为 GET 方法
in the view_task.php page the $ _POST ['report_id'] section is not defined after the data update, because in update_task_name.php you use a redirect header ("Location: view_task.php") which becomes the GET method
尝试更新
update_task_name.php
update_task_name.php
header ("Location: view_task.php?report_id=". $ _ POST ['report_id'])
view_task.php//第 7 行
view_task.php //line 7
if (isset ($_POST['report_id'])) {
$report_id = $_POST['report_id'];
} else if (isset ($_GET['report_id'])) {
$report_id = $_GET['report_id'];
} else {
die ("ID NOT DEFINED");
}
这篇关于PHP/MySQL:成功更新数据后显示一些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!