这是我的php代码
foreach...
$sql1 = "INSERT INTO health_declaration (question_id,patient_id,answer) VALUES ('$question','$patient_id','$answer')";
$query_run = mysqli_query($conn,$sql1);
如何将while循环中的单选按钮数据插入PHP数据库?基本上,我想把答案保存到数据库中。我已经在这里搜索了答案,但关于这个主题的文章有限。
<?php
$sql = "SELECT * FROM questionnaires";
$query_run = mysqli_query($conn,$sql);
$check_services = mysqli_num_rows($query_run) > 0;
if($check_services)
{
while($row = mysqli_fetch_array($query_run))
{
?>
<div class="form-group">
<label for=""><?=$row['questions']?> *</label>
<div class="form-check">
<input class="form-check-input" type="radio" name="q[<?=$row['id']?>]" value="Yes">
<label class="form-check-label">Yes</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="q[<?=$row['id']?>]" value="No">
<label class="form-check-label" value="No">No</label>
</div>
</div>
<?php
}
}
else
{
echo "<h5> No Record Found</h5>";
}
?>
你可以用一个简单的方法来做
foreach($_POST as $key => $value) {
$answers = [];
// check if name key start with q, i.e. q1
if(substr($key ,0, 1) == 'q')
{
$answers[$key] = $value;
}
}
var_dump($answers);
// now you can do what you want with your answers the same way like you read then (foreach)
希望这有帮助。下次请把你试过的贴出来。问题不会传递到您发布的代码。