php - 读出数据并编辑
问题描述
我想从表中读出所有数据记录,并根据它们的“id”在“状态”中为各个数据记录赋予一个新值。
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm@X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th><button type="submit" name="change">CHANGE STATUS</button></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<?php
if (isset($_POST['change'])) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>
我的问题是,当我单击按钮时,所有记录都被更改,而不仅仅是一个。我希望有人可以帮助解决问题。
..................................................... ..................................................... ..................................................... ..................................................... .
解决方案
尝试使用 {} 将数组元素嵌入到字符串中并包含“id”值
$update = "UPDATE data Set status = '0' WHERE id = {$row['id']}"
否则,您的查询将解释$row
和[id]
作为单独的项目:
UPDATE data Set status = '0' WHERE id = [id]
结果,您的所有行都更新了
还要考虑将状态更新按钮应用于每一行,如果不发送将更新哪个行 id 的参数,则无法使用一般的“更新状态”按钮仅更新一行。例如:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm@X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<button type="submit" name="change" value="<?php echo $row['id']; ?>">CHANGE STATUS</button>
<?php
if (isset($_POST['change']) && $_POST["change"]==$row['id']) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>
推荐阅读
- python - 多线程时循环不使用新变量运行
- java - 我们使用@inject 时的默认范围是什么
- ruby-on-rails - 如何诊断消失的 Flash 消息?
- google-chrome-extension - 扩展程序可以关闭它自己的由 browserAction 或 pageAction 打开的弹出窗口吗?
- python - 如何将 Python pandas 函数作为变量/参数传递给另一个函数?
- sublimetext3 - ST3 - 将选择扩展到 Word 并快速查找下一个 - 在 Sublime Text 3.1.1 (B3176) 中的 Mac OS 上 cmd + d 的双重分配
- php - 在字符串中特定文本的末尾位置添加字符串
- bash - 自动 scp 文件传输 - 在 Synology DS 和 dd-wrt 路由器之间
- dart - 具有命名构造函数的通用类型的 Dart 抽象类
- javascript - 如何在节点快递中循环调用服务?