在SQL中显示对与错选择查询为复选框

时间:2018-12-03 14:56:50

标签: php mysql select mysqli checkbox

当前状态是此代码:

while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['timeStamp'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['what'] . "</td>";
echo "<td>" . $row['salad'] . "</td>";
echo "<td>" . $row['tomatoes'] . "</td>";
echo "<td>" . $row['onions'] . "</td>";
echo "<td>" . $row['carrots'] . "</td>";
echo "<td>" . $row['hot'] . "</td>";
echo "<td>" . $row['cheese'] . "</td>";
echo "<td>" . $row['sauce'] . "</td>";
echo "</tr>";

沙拉,西红柿,洋葱,胡萝卜,热和奶酪这些行被保存为真和假,因为用户将它们作为复选框插入。 我基本上想要的是在写入sql结果的表中显示复选框时为true或false

这有可能吗? 让我知道您是否需要更多代码(例如整个php)。

2 个答案:

答案 0 :(得分:0)

如果我理解的没错,您可以简单地添加一个复选框并设置被检查的属性(如果值是true,如下所示)

echo '<td><input type="checkbox" value="true" ';
echo ($row['salad'])? 'checked' : '';
echo '></td>';

请注意,如果您不希望用户对其进行编辑或取消选中它,则可以添加已禁用属性。

答案 1 :(得分:0)

假设您已为每个项目将值保存为db中的truefalse

while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['timeStamp'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['what'] . "</td>";
echo "<td><input type='checkbox' " . ($row['salad']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['tomatoes']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['onions']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['carrots']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['hot']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['cheese']=='true' ? 'checked' :'') . "/></td>";
echo "<td><input type='checkbox' " . ($row['sauce']=='true' ? 'checked' :'') . "/></td>";
echo "</tr>";