php - $_SESSION['message] 在使用 $_GET 时不起作用
问题描述
我遵循了 CRUD 教程(https://www.youtube.com/watch?v=3xRMUDC74Cw&ab_channel=CleverTechie)并且在我删除记录时一切正常,但删除消息。我认为按钮类危险可能由于某种原因不起作用(过时或 w/e),并试图将其更改为其他东西,但它仍然不起作用。
现在我相信这与我使用 $_GET 方法删除记录的事实有关,但我不再确定了。成功消息和更新消息都工作正常,但删除消息不是。
索引.php
<?php require_once 'process.php'; ?>
<?php if(isset($_SESSION['message'])): ?>
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>
<div class="container">
<?php
$mysqli = new mysqli('localhost', 'root', '', 'dbase') or die(mysqli_error($mysqli));
$result = $mysqli->query("SELECT * FROM data") or die($mysqli_error);
//pre_r($result);
?>
<div class="row justify-content-center">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Location</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['location']; ?></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>"
class="btn btn-info">Edit
</a>
<a href="index.php?delete=<?php echo $row['id']; ?>"
class="btn btn-danger">Delete
</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php
//pre_r($result->fetch_assoc());
//pre_r($result->fetch_assoc());
function pre_r($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
<div class="row justify-content-center">
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control"
value="<?php echo $name; ?>" placeholder="Enter your name">
</div>
<div class="form-group">
<label>Location</label>
<input type="text" name="location" class="form-control"
value="<?php echo $location; ?>" placeholder="Enter your location">
</div>
<div class="form-group">
<?php
if ($update == true):
?>
<button type="submit" class="btn btn-info" name="update">Update</button>
<?php else: ?>
<button type="submit" class="btn btn-primary" name="save">Save</button>
<?php endif; ?>
</div>
</form>
</div>
</div>
进程.php
<?php
session_start();
$mysqli = new mysqli('localhost', 'root', '', 'pdlenart') or die(mysqli_error($mysqli));
$update = false;
$id = 0;
$name = "";
$location = "";
if (isset($_POST['save'])) {
$name = $_POST['name'];
$location = $_POST['location'];
$mysqli->query("INSERT INTO data (name, location) VALUES('$name', '$location')") or die($mysqli_error);
$_SESSION['message'] = "Record has been saved!";
$_SESSION['msg_type'] = "success";
header("location: index.php");
}
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$mysqli->query("DELETE FROM data WHERE id=$id") or die($mysqli_error);
$_SESSION['message'] = "Record has been deleted!";
$_SESSION['msg_type'] = "danger";
header("location: index.php");
}
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$result = $mysqli->query("SELECT * FROM data WHERE id=$id") or die($mysqli_error);
if (count($result)==1) {
$row = $result->fetch_array();
$name = $row['name'];
$location = $row['location'];
}
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$location = $_POST['location'];
$mysqli->query("UPDATE data SET name='$name', location='location' WHERE id=$id") or die($mysqli_error);
$_SESSION['message'] = "Record has been updated!";
$_SESSION['msg_type'] = "warning";
header("location: index.php");
}
编辑:我已经通过删除 header(location: index.php); 在 if (isset($_GET['delete'])) {} 部分。不知道发生了什么或为什么。
此外,我会记下有关 SQL 注入的评论,但这就是为什么某些功能无法正常工作或只是通过删除标头(位置..)而发生的情况。
解决方案
推荐阅读
- javascript - Angular 构建产品在运行时失败
- winforms - 写入日志以到达带有 serilog 扩展名的文本框
- reactjs - 当用户选择相同的选项时反应选择 onChange 触发器
- python - 您如何将 alpha vantage 日期设置为列?
- android - Use android dependency from Azure DevOps public artifact
- java - java - 如何使用secp256r1椭圆曲线密钥对在java中执行加密和解密?
- wso2 - WSO2 VFS FTP 代理动态创建
- amazon-web-services - 有没有办法在夜间触发 cloudwatch 警报时忽略它们?
- nginx - Lua body_data() 返回 nil 虽然请求正文不为空
- python - 使用 Pandas 修改 Excel 文件,布局更改最少