如何使用 PHP 将单选按钮记录添加到 MySQL 字段中 [英] How can i add radio buttons record into MySQL fields using PHP
问题描述
谁能告诉我 PHP 代码和 MySQL 查询,以便我可以将单选按钮数据发布到 MySQL 字段中?我有一个名为出勤"的 MySQL 表,它有四个字段:
Can somebody tell me the PHP code and MySQL query so I can post radio buttons data into MySQL fields? I've a MySQL table called "Attendance" which has four fields:
- 现在
- 缺席
- 离开
- 假期
我的 HTML 表单也包含四个单选按钮
My HTML form contain four radio buttons as well like
<input type="radio" name="present" value="1" />
<input type="radio" name="absent" value="2" />
<input type="radio" name="leave" value="3" />
<input type="radio" name="holiday" value="4" />
我想要的是,如果有人选择缺席单选按钮,那么2应该去考勤表中的缺席字段,其他3个字段在表中将为空;如果有人选择出席,则出勤中的1应进入出席字段,其他3个缺勤假期字段将为空.
What I want is that if someone select absent radio button, then 2 should go to absent field in Attendance Table and other 3 fields will be empty in table; if someone select present then 1 should go to present field in Attendance and other 3 fields of absent leave holiday will be empty.
还有 4 个单选按钮的名称不同,所以存在问题,我可以选择所有 4 个单选按钮.我怎样才能让他们从 4 个中选择一个?我知道如果名称相同,那么我可以选择 1 但这里是不同的问题,4 个不同的字段.
Also names are differents of 4 radio buttons, so there is problem that i can select all 4 radio buttons. How can I make them select one out of 4? I know if names are same then I have choice to select 1 but here is different issue, 4 different fields.
推荐答案
为无线电组的所有元素指定相同的名称.这将为 HTML 部分执行此操作.在 PHP 中,您可以根据 REQUEST 值进行简单的切换:
Give all elements of the radio group the same name. This will do it for the HTML part. In PHP you do a simple switch depending on the REQUEST value:
<?php
$defaultInsertData = array(0,0,0,0);
$currentValue = intval($_REQUEST['FIELDNAME']);
switch($currentValue) {
case 1:
$defaultInsertData[0] = 1;
break;
case 2:
$defaultInsertData[1] = 2;
break;
case 3:
$defaultInsertData[2] = 3;
break;
case 4:
$defaultInsertData[3] = 4;
break;
}
// and here you have your array with the four elements....
var_dump($currentValue);
?>
这篇关于如何使用 PHP 将单选按钮记录添加到 MySQL 字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!