我想为我的表单设置单选按钮的默认值。 默认值应与数据库中的值匹配。 我知道如何通过使用 checked="checked" 来设置我的默认值。 就我而言,问题在于如何在单选按钮中使用数据库值作为默认值。
<tr>
<td>ASP.NET</td>
<td><input type="radio" name="asp" value="not at all competent" ></td>
<td><input type="radio" name="asp" value="little competent" ></td>
<td><input type="radio" name="asp" value="moderately competent"</td>
<td><input type="radio" name="asp" value="extremely competent"></td>
</tr>
您可以像下面这样操作:
<?php
//Retrieve your value here
$query="select * from technical_skills where student_id='$student_id'";
$result=mysqli_query($conn,$query);
$row=mysqli_fetch_array($result);
// Assign the column to the variable
$dbValue = $row['columnname'];
?>
<tr>
<td>ASP.NET</td>
<td><input type="radio" name="asp" value="not at all competent"<?php echo ($dbValue=='not at all competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="little competent"<?php echo ($dbValue=='little competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="moderately competent"<?php echo ($dbValue=='moderately competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="extremely competent"<?php echo ($dbValue=='extremely competent'?' checked=checked':''); ?> ></td>
</tr>
您可以为每个选项定义一个空变量,并根据数据库返回值将它们设置为"checked=checked"。
例:
$not_competent = ''
if($dbResult == 'not_competent') $not_competent = 'checked=checked';
对其他选项执行相同的操作,然后打印表。
将表写在"..."所以 PHP 可以对变量进行修改并显示 正确的信息
print "<tr>
<td>ASP.NET</td>
<td><input type='radio' name='asp' value='not at all competent' $not_competent></td>
<td><input type='radio' name='asp' value='little competent' $little competent></td>
<td><input type='radio' name='asp' value='moderately competent' $moderately_competent></td>
<td><input type='radio' name='asp' value='extremely competent' $extremely_competent></td>
</tr>";
干杯!