php - 带有while循环的PHP表单
问题描述
我在生成可以基于 SQL 查询打印表单的页面时遇到了一些麻烦。在这种情况下,我需要执行查询以列出名称为 Porcac1x 的表的所有行。内容需要是显示当前变量值并且可以更新的输入字段。这正是我卡住的地方。如何创建一个基于 php while 循环的可变表单?使用附加的代码,我可以列出内容并显示所有变量,但是在创建表单操作以更新值时遇到了麻烦。我想明确一点,我不关心安全性,因为代码将在本地环境中运行,我是唯一可以访问的人。
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>test page</title>
</head>
<body><form action="" method="post">
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "root";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `id`, `title`, `amount` FROM `expenses` WHERE name='Porcac1x';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "ID: <input type='text' name='".$row["id"]."' value='".$row["id"]."'> Title: <input type='text' name='".$row["title"]."' value='".$row["title"]."'> Amount: <input type='text' name='".$row["amount"]."' value='".$row["amount"]."'> <button type='submit' name='save'>Save</button><br>";
}
} else {
echo "0 results";
}
if(isset($_POST['save'])){
$myID = $_POST["id"];//??? < Issue
$myTitle = $_POST["title"];//??? < Issue
$myAmount = $_POST["amount"]; //??? < Issue
echo $myID;
echo $myTitle;
echo $myAmount;
$sqlUpdate = "UPDATE expenses SET title='$myTitle', amount ='$myAmount' WHERE id='$myID';";
echo $sqlUpdate;
if ($conn->multi_query($sqlUpdate) === TRUE) {
echo "Record updated successfully";
$risposta= "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
$risposta= "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</form>
</body>
</html>
解决方案
就像我在评论中所说的那样,你需要数组表示法。(或每行 1 个表格)
这是每行 1 个表单元素的解决方案:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>test page</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "root";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `id`, `title`, `amount` FROM `expenses` WHERE name='Porcac1x';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<form method="post">';
echo "ID: <input type='text' name='id' value='".$row["id"]."'> Title: <input type='text' name='title' value='".$row["title"]."'> Amount: <input type='text' name='amount' value='".$row["amount"]."'> <button type='submit' name='save'>Save</button><br>";
echo '</form>';
}
} else {
echo "0 results";
}
if(isset($_POST['save'])){
$myID = $_POST["id"];//??? < Issue
$myTitle = $_POST["title"];//??? < Issue
$myAmount = $_POST["amount"]; //??? < Issue
echo $myID;
echo $myTitle;
echo $myAmount;
$sqlUpdate = "UPDATE expenses SET title='$myTitle', amount ='$myAmount' WHERE id='$myID';";
echo $sqlUpdate;
if ($conn->multi_query($sqlUpdate) === TRUE) {
echo "Record updated successfully";
$risposta= "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
$risposta= "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
推荐阅读
- python - 逐列验证并插入更新列值
- forms - 如何防止所有表单字段在填写单个字段时进行验证
- sql - 如何选择过去 13 个月的数据?
- php - 有没有办法在php中对异步函数进行测试?
- dart - 转换流
- > 流式传输
- azure - 通过 Bastion 提供对外部身份以访问 Azure VM 的最佳和最安全的方法是什么?
- flutter - 如何使用 Bloc/Cubit 更新 Flutter 中的列表?
- robotframework - 尝试使用 Robot Framework 中的 SapGuiLibrary 与 SAP 元素交互时出错
- php - 发送大量信息后如何整理我的网址?
- amazon-web-services - 基于标签保护 AWS 资源