How can i add radio buttons record into MySQL fields using PHP

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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:

  • present
  • absent
  • leave
  • holiday

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" />

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.

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.

回答1:

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); ?>


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!