如何在数据库中插入此表单的值?
<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>
因为当我要将这些数据插入数据库时,它只插入第二个表行中的最后一个值“有烟道”....
$medical = new MedicalHistory;
$medical->username = $value;
$medical->question = $data['question'];
$medical->answer = $data['answer'];
$medical->remarks = $data['remarks1'];
$medical->save();
我使用laravel作为框架
答案 0 :(得分:1)
仅插入第二个表单的原因是因为您正在为字段重复使用相同的名称。
一种解决方案是为所有字段提供唯一的名称(单选按钮对除外)。
更好的解决方案是制作阵列输入,我已经快速搜索了你并想出了这个:How to get form input array into PHP array
您应该将代码修改为数组变量,因此如果您想保存多个问题,请在两个问题字段中使用名称'question []'而不是'question'。
前端表示例:
<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>
要在PHP逻辑中处理数据的新格式,您应该执行以下操作:
$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";
}
由于您要保存数据,您应该执行以下操作:
$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();
}
在我编辑之后,它现在应该可以正常工作。
答案 1 :(得分:0)
你的表格应该是这样的:
<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();
?>
实际上你的表单元素被覆盖了。
由于 阿米特