im试图通过$_GET获取帖子的id来更新记录,然后通过$_post更新记录。
我已经通过$_GET执行了删除操作,它也很好用,mysqli_fetch_assoc也很好地显示要编辑的记录,但实际的编辑没有发生——它在空检查函数中的代码验证中给出了一个空错误。
我已经做了很多研究,但似乎无法理解这个错误,如果有人能建议对代码进行任何更改,我将非常感谢。
提前谢谢!
这是错误
Notice: Undefined index: id in /then the long url etc/
以下是代码
<?php
//DB Connection
include'include/db-conn.php';
if (isset($_POST['edit'])) {
//Raw GET Inputs
$raw_c_id = $_GET['id'];
//Cleaned Inputs
$c_c_id = filter_var($raw_c_id, FILTER_SANITIZE_STRING);
//Error Mwssages
$empty = '<div class="alert alert-danger alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Error!</strong>Field is empty please provide content!
</div>
';
$success = '<div class="alert alert-success alert-dismissible fixed-top">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> Content Added Successfully
</div>
';
$not_success = '<div class="alert alert-danger alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Not Success!</strong> Content Not Added Successfully
</div>
';
if (empty($c_c_id)) {
echo $empty;
exit();
header("Location:index.php");
}
$update = "UPDATE `continents`
SET `continent_name`='$c_c_name', `last_edited`='date(d/m/Y)'
WHERE `id`='$c_c_id'";
$run_update = mysqli_query($conn, $update);
if (!$run_update) {
header("Location: index.php");
echo $not_success;
}
else{
header("Location: index.php");
echo $success;
}
}
?>
这是html部分
<div class="panel-body">
<form action="edit.php" method="POST">
<div class="form-group">
<label for="continent_name">Continent Name</label>
<input required type="text" placeholder="10" class="form-control" value="<?php echo $c_name ; ?>" name="continent_name">
</div>
<small>Date Added: <?php echo $c_dated_added ; ?></small> / <small>Last Edited: <?php echo $c_last_edited ; ?></small>
<div class="form-group">
<input class="form-control btn-success" type="submit" name="edit" value="Submit">
</div>
</form>
</div>
Thid the while loop
<div class="table-responsive">
<table id="example" class="table table-hover ">
<thead>
<tr class="">
<th>ID</th>
<th>Continent Name</th>
<th>Date Added</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$all_continents = "SELECT * FROM `continents` ORDER BY `status`";
$run = mysqli_query($conn,$all_continents);
while ($row_result = mysqli_fetch_assoc($run)) {
$id = $row_result['id'];
$c_continent_name = $row_result['continent_name'];
$c_date_added = $row_result['date_added'];
$c_status = $row_result['status'];
echo "
<tr>
<td>$id</td>
<td>$c_continent_name</td>
<td>$c_date_added</td>
<td>$c_status</td>
<td>
<a class='btn btn-info' href='edit.php?id=$id'>Edit</a>
</td>
<td>
<a class='btn btn-danger' href='delete.php?id=$id'>Delete</a>
</td>
</tr>
";
}
?>
</tbody>
</table>
看起来您正试图同时使用GET和POST参数。它不起作用的原因是当你提交表单时,GET参数丢失了。你需要在表单的动作属性中传递它:
<form action="edit.php?id=<?php echo $_GET['id'] ?>" method="POST">
也请看一下这篇文章中的建议:有没有一种方法可以同时使用GET和POST?