How could I display a checked checkbox based from it's value in the database?
I'm new in PHP, could you help me s开发者_如何学JAVAolve my problem? I'm trying to display the checkbox checked based on its value in the database. I have saved its value as 1 if it's checked and 0 if it's not.
<?php
$sql = "SELECT somecol FROM sometable";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$checked = $result['somecol'];
?>
<input type="checkbox" name="somecol" value="1" <?php if ($checked == 1) echo 'checked'; ?> />
You can test the field, let's say $row['col']
, and emit checked="checked"
if the field contains 1.
echo '<input type="checkbox" name="n" value="v"' . ($row['col']==1 ? ' checked="checked"' : '') . '>';
- Use checked="checked"
精彩评论