【发布时间】:2015-09-26 07:14:40
【问题描述】:
我有一个 MySql 表,我可以通过 HTML 表单进行修改。 MySql 表 有这样的列:
id (int) | name (text) | option1 (boolean) | option2 (boolean)
options 字段的数量会随着时间的推移而增长,并且不固定,但始终包含布尔数据(0 或 1)。行数也不固定。
我想用 html 表单修改表格,如下所示:
<form action="file.php" method="post">
<table style="width:100%">
<tr>
<th>Name</th>
<th>Option1</th>
<th>Option2</th>
</tr>
<tr>
<th>lili</th>
<th><input type="checkbox" name="lili_1"checked></th>
<th><input type="checkbox" name="lili_2"></th>
</tr>
<tr>
<th>thor</th>
<th><input type="checkbox" name="thor_1" checked></th>
<th><input type="checkbox" name="thor_2" checked></th>
</tr>
<tr>
<th>fred</th>
<th><input type="checkbox" name="fred_1" checked></th>
<th><input type="checkbox" name="fred_2" ></th>
</tr>
</table>
<button type="submit">Update</button>
</form>
现在变得棘手了。当我提交表单时,在服务器端我不知道有多少列/行。我想更新整个数据库表(它很小),所以我需要知道列(option1、option2、...)和行(lili、thor、fred)的每个复选框的状态 ,...)
我的想法是根据字段“名称”的列和值来命名每个复选框。我还需要发送一个包含所有名称和列的数组(因为我只能知道被选中的框,而不是未选中的框)。
然后,在服务器端,我需要使用列和名称的数组重新创建矩阵,并将 1 放在选中的复选框(通过复选框的名称)。
这似乎是一个非常容易出错且执行时间很长的操作...要完成如此简单的任务。有人可以指出我更聪明的方法吗?
谢谢
【问题讨论】:
-
对整个“lili”组尝试
name="lili[]",对所有其他组尝试相同。在服务器端,您将获得一个名为“lili”($_POST['lili'])的列表,其中包含 True/False 值。 -
我用 html 按钮提交表单
-
你确定吗?我不会只得到“真实”的价值观吗?因为只有实际选中的复选框才会发送数据?
-
是的,没错。您还需要在复选框上设置
value="1/2/3/..."。然后,您将获得一个“id”列表,而不仅仅是true值,以便您识别它们。 -
其实这里已经有答案了stackoverflow.com/questions/4997252/…