Checkbox values from form into database ... AND BACK?

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
There's a lot of ways to store checkbox values in a database (e.g. ENUM('yes', 'no') or just BOOL), but you've got the right idea.

Retrieving them from the database is easy. You do it just like any other query. Then when you get the value you you can just test whether it's set to 1, and if it is, make the checkbox "checked". In these cases, it's sometimes handy to use the ternary operator (?) to avoid excessive if()s. Something like this:

Code:
<?php
$result = mysql_query("SELECT checkbox1 FROM table WHERE ...");
while($row = mysql_fetch_assoc($result)) {
  echo '<input type="checkbox"' .
    $row['checkbox1'] ? ' checked="checked"' : ''
  '/>';
}
?>
 
Back
Top