0

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
  • It seems redundant to store the value "2" in the "absent" column - it would be somewhat better to make each of these a boolean, since a person is either absent or they are not - the value "2" has nothing to do with it. It would be even better to swap all four fields for one called "status", which takes four values: P, A, L or H. This prevents a situation in your database where a person is recorded as both present and on holiday, which presumably is impossible. Commented Dec 27, 2012 at 20:11

1 Answer 1

2

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);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

above code is fully working, list of 10+ students attendance is going to field of table student_attendance called current indivual with no problem, i have now split the current field to 4 fields called p, a, l, h, so what i want is if we select present radio option for first student value 1 should go to p field and rest 3 will be empty as they are Null by default. Reason is i want to calculate percent of student attendances according to presents and leaves so i want this thing to go forward rest of other code i know what to do, just solve my this issue
When I see your provided code, I want to point out, that it is highly insecure and easy to attack. Anyway, basicly what you could do is, dynamic your VALUES part of the statement like this: $statementValues = array('"'.$VALUE_1.'"', '"'.$VALUE_2.'"', ...). As explained in the code example above you set the array values for the four different variants depending on the returning REQUEST value (they must differ, otherwise you can't do that), and finally you convert your $statementValues array to string by doing implode(", ", $statementValues); and append it to the SQL statement.
can you please make changes in code and give me, getting problem on how to add code....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.