0

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

inverted_index
  • 2,329
  • 21
  • 40

2 Answers2

1

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\n";
}

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.

Community
  • 1
  • 1
S. Kerdel
  • 90
  • 1
  • 9
  • Will not work like that - as they are radio buttons with the same name they are effectively the same element - if you select one for the first question then select one for the second question only the latter selection will be selected... – Professor Abronsius Feb 05 '16 at 12:30
  • One moment, ill update my answer; didn't think about that. – S. Kerdel Feb 05 '16 at 12:34
  • @RamRaider I've modified my code, it should work as it should now; thanks! – S. Kerdel Feb 05 '16 at 12:39
0

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

Amit Shah
  • 1,380
  • 1
  • 10
  • 19