bilcker bilcker - 4 months ago 6
HTML Question

Checking checkboxes from database values

I post values from check boxes into a database for a user profile. When the user goes to edit his/her profile I want the checkboxes that they selected previously to be checked so they don't lose that info after updating their profile. I have tried many different solutions but with no luck.

The check box values get entered into table name members_teachers into a column called focus and are seperated by a comma for example art,mathematics,dance etc I am not sure how close or far I am away at accomplishing my goal but I greatly appreicate any help or suggestions you can provide. Thank you very much in advance

My code to try and check the values is

$focusQuery = mysql_query("SELECT focus FROM members_teachers WHERE id = $member_id") or die;

while ($new_row = mysql_fetch_assoc($focusQuery)){

$focusRow = $row['focus'];

$focusValue = explode(',', $focusRow);

foreach($focusValue as $newFocus){

//echo $newFocus;

//echo "<br/>";

$result = mysql_query("SELECT focus FROM members_teachers WHERE focus LIKE '%$focusRow%'") or die;

if(mysql_num_rows($result) > $newFocus){

$checked = 'checked="checked"';



$checked = '';




This is my html

<label for="art-focus">Art</label>
<input name="focus[]" type="checkbox" value="Art" <?php echo $checked ?>>

<label for="math-focus">Mathematics</label>
<input name="focus[]" type="checkbox" value="Mathematics" <?php echo $checked ?>>

<label for="dance-focus">Dance</label>
<input name="focus[]" type="checkbox" value="Dance" <?php echo $checked ?>>

// Create connection

// Check connection
if (mysqli_connect_errno($con))
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT focus FROM members_teachers WHERE id = $member_id"); 
while($row = mysqli_fetch_array($result))

<input type="checkbox" name="focus[]" value="Art" <?php if(in_array("Art",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Mathematics" <?php if(in_array("Mathematics",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Dance" <?php if(in_array("Dance",$focus)) { ?> checked="checked" <?php } ?> >