将表单值插入数据库

How do I insert the value of this forms in the database?

<tr>
    <td>1. Do you feel well and healthy?</label></td>
    <input type="hidden" name="question" value="1. Do you feel well and healthy?">
    <td><input type="radio" name="answer" value="Yes" checked></td>
    <td><input type="radio"  name="answer" value="No"></td>
    <td><input type="text" name="remarks"></td>
</tr>
<tr>
    <td><ul><li>Have any flu or cold?</li></ul></td>
    <input type="hidden" name="question" value="Have any flu or cold?">
    <td><input type="radio" name="answer" value="Yes" checked> </td>
    <td><input type="radio"  name="answer" value="No"></td>
    <td><input type="text" name="remarks"></td>
</tr>

Because when i'm to insert this data to the database it only inserts the last value which is in the 2nd table row "have flue"....

$medical = new MedicalHistory;
$medical->username = $value;
$medical->question = $data['question'];
$medical->answer   = $data['answer'];
$medical->remarks  = $data['remarks1'];
$medical->save();

I am using laravel as a framework

Your form should be like this:

<tr>
    <td>1. Do you feel well and healthy?</label></td>
    <input type="hidden" name="question" value="1. Do you feel well and healthy?">
    <td><input type="radio" name="answer" value="Yes" checked></td>
    <td><input type="radio"  name="answer" value="No"></td>
    <td><input type="text" name="remarks"></td>
</tr>
<tr>
    <td><ul><li>Have any flu or cold?</li></ul></td>
    <input type="hidden" name="question2" value="Have any flu or cold?">
    <td><input type="radio" name="answer2" value="Yes" checked> </td>
    <td><input type="radio"  name="answer2" value="No"></td>
    <td><input type="text" name="remarks2"></td>
</tr>
<?php
$medical = new MedicalHistory;
$medical->username = $value;
$medical->question = $data['question'];
$medical->answer   = $data['answer'];
$medical->remarks  = $data['remarks1'];
$medical->save();

$medical = new MedicalHistory;
$medical->username = $value;
$medical->question = $data['question2'];
$medical->answer   = $data['answer2'];
$medical->remarks  = $data['remarks2'];
$medical->save();
?>

Actually your form elements are getting overridden.

Thanks Amit

The reason only the second form is being inserted is because you are reusing the same names for your fields.

One solution will be giving all your fields giving unique names (except the radio button pairs).

A better solution would be making array inputs, i've did a quick search for you and came up with this: How to get form input array into PHP array

You should modify your code to array variables, so if you would like to save multiple questions, use in both question fields the name 'question[]' instead of 'question'.

Front-end table example:

<tr>
    <td>1. Do you feel well and healthy?</label></td>
    <input type="hidden" name="question[]" value="1. Do you feel well and healthy?">
    <td><input type="radio" name="answer[0]" value="Yes" checked></td>
    <td><input type="radio"  name="answer[0]" value="No"></td>
    <td><input type="text" name="remarks[]"></td>
</tr>
<tr>
    <td><ul><li>Have any flu or cold?</li></ul></td>
    <input type="hidden" name="question[]" value="Have any flu or cold?">
    <td><input type="radio" name="answer[1]" value="Yes" checked> </td>
    <td><input type="radio"  name="answer[1]" value="No"></td>
    <td><input type="text" name="remarks[]"></td>
</tr>

To handle the new format of your data in the PHP logic you should do something like this:

$question = $_POST['question'];
$answer = $_POST['answer'];
$remark = $_POST['remarks'];

foreach( $question as $key => $q ) {
    print "The question is ".$q.", answer is ".$answer[$key].
          ", and remarks are ".$remark[$key].". Thank you
";
}

Since you want to save your data, you should do something like this:

$value = 'idk_where_you_assign_names';
$question = $_POST['question'];
$answer = $_POST['answer'];
$remark = $_POST['remarks'];

foreach( $question as $key => $q ) {
    $medical = new MedicalHistory;
    $medical->username = $value;
    $medical->question = $q;
    $medical->answer   = $answer[$key];
    $medical->remarks  = $remark[$key];
    $medical->save();
}

After my edits it should work now.