将单选按钮的值发送到数据库的不同字段
我有一个提交表单,其中包含 1 组单选按钮。
<div id="defectclass">
<input id="def1" type="radio" class="defect" name="defect" value="1"/>S
<input id="def2" type="radio" class="defect" name="defect" value="1" />A
<input id="def3" type="radio" class="defect" name="defect" value="1" />B
<input id="def4" type="radio" class="defect" name="defect" value="1" />C
</div>
除此之外,我在数据库表中有4个字段,即:
- S
- A
- B
- C
我在提交后想要:
- if def1 are checked send value to field "S"
- if def2 are checked send value to field "A"
- if def3 are checked send value to field "B"
- if def4 are checked send value to field "C"
- if all not checked or null send to all fields value="0"
我该怎么做,因为我从未尝试过这个?
i have a submit form that consist of 1 group radio button.
<div id="defectclass">
<input id="def1" type="radio" class="defect" name="defect" value="1"/>S
<input id="def2" type="radio" class="defect" name="defect" value="1" />A
<input id="def3" type="radio" class="defect" name="defect" value="1" />B
<input id="def4" type="radio" class="defect" name="defect" value="1" />C
</div>
beside that i have 4 fields at DB table, that is :
- S
- A
- B
- C
i want after submit:
- if def1 are checked send value to field "S"
- if def2 are checked send value to field "A"
- if def3 are checked send value to field "B"
- if def4 are checked send value to field "C"
- if all not checked or null send to all fields value="0"
how do i do that bcoz i've never try this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样,
这个数组可以在许多任务中使用:
所以,在收到 POST 数据后,只需将其与此数组进行比较:
您将在 $key 中看到一个数字多变的。该号码应存储在数据库中。
like this
this array can be used in many tasks:
So, upon receiving your POST data just compare it against this array:
and you will have a number in the $key variable. This number should be stored in the database.
这将为您提供 $_POST['defect']['s'], &tc。这样您就会知道要采取哪条更新路径。有关此语法的更多信息,请参阅 http://php.net/faq.html。
This will give you $_POST['defect']['s'], &tc. so you'll know which update path to take. See http://php.net/faq.html for more about this syntax.
通过查看
defect
的可能值,在表定义中将defect VARCHAR(1)
更改为defect VARCHAR(1)DEFAULT "0"
>这简化了您在 php 脚本中插入行/更新列的代码
,解析表单后,添加 switch 语句来决定更新哪一列。
By looking at the possible values of
defect
, in your table definition,change
defect VARCHAR(1)
todefect VARCHAR(1) DEFAULT "0"
This simplifies your code to insert row/update columns
in your php script, after parsing the form, add a switch statement to decide which column to update.