我在这里发布这个问题是因为我有点迷失。这是我第一次使用 PHP 和 MySQL,这是我为大学作业做的。我已经成功满足了所有 CSS、Javascript 和部分 PHP/MySQL 要求。我唯一剩下的就是:
如何从通过 PHP 和 MYSQLquery 生成的表单中获取值并更新 MySQL 表的相应行。为了更容易理解,我附上了一张图片。
左侧允许用户搜索或显示所有订单。然后内部的 PHP 将调用自身来更新右侧的表。那部分我设法做到了。但现在我的作业要求的一部分是:
The Manager should be able to ‘update’ the status of an order from a link
or button next to the order in the table, changing the status from
(pending | fulfilled | paid | archived). This could be done by returning
the record to a form in this webpage, to change and ‘confirm’ the change.
现在生成的表格实际上是一个表单。我在这里放了一些代码片段
echo "<form method=\"post\" id=\"regForm\" action=\"https://formtest.php\" name=\"editAll\">\n";
.......
echo "<td>", $row["order_status"],"</td>\n";
echo "<td><select name=\"change_orderstatus[]\" id=\"change_orderstatus[]\">
<option value=\"\" selected=\"selected\" >Please Select</option>
<option value=\"pending\">Pending</option>
<option value=\"Fulfilled\">Fulfilled</option>
<option value=\"Paid\">Paid</option>
<option value=\"Archived\">Archived</option>
<option value=\"Delete\">Delete</option>
</select></td>\n";
....
echo "<input type= \"submit\" value=\"Save changes\" id=\"commit\" name=\"editAllquery\" />";
如果我放入change_orderstatus[]
,它应该会变成一个数组,但我如何利用它来更新我的 MySQL 表
我附加了另一张图片以显示底部有保存更改按钮
现在我知道 Stackoverflow 以攻击别人而不是提供帮助而闻名,因为“所有菜鸟都应该学习他们的东西”(这是我个人的经验),但我并不是要求你为我编写代码或帮我做作业。
我只是请你为我指明一个方向,然后我就可以自己做这件事。就这样。我试图让我的帖子尽可能清晰和详细,这样我就不会立即被否决并被遗忘。
如果你能帮助我,那就太好了。 谢谢
最佳答案
首先,您需要在某处输出一个表单,我建议将其设置为:
<form action="phphandler.php" id="form1" method="POST" style="display: none;">
<input type="hidden" name="id" id="inp1">
<input type="hidden" name="status" id="inp2">
</form>
为选择创建onChange事件:
<select onchange="event.preventDefault(); myFunction(1)" id="sel1">
应该使用 php 为每个选择生成具有唯一 ID 的 myFunction(1)
然后函数如下:
function myFunction(id) {
document.getElementById('inp1').value = id;
document.getElementById('inp2').value = document.getElementById('sel1').value;
document.getElementById('form1').submit()
}
除了 inp1、inp2 和 form1 之外,所有 ID 均由 php 生成
关于php - 从下拉列表和表单更新 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44044735/