【问题标题】:Checking checkboxes from database values从数据库值中检查复选框
【发布时间】:2013-08-27 18:02:18
【问题描述】:

我将复选框中的值发布到用户配置文件的数据库中。当用户去编辑他/她的个人资料时,我希望他们之前选择的复选框被选中,这样他们就不会在更新他们的个人资料后丢失该信息。我尝试了许多不同的解决方案,但都没有成功。

复选框值被输入到表名 members_teachers 到名为 focus 的列中,并用逗号分隔,例如艺术、数学、舞蹈等欣赏您可以提供的任何帮助或建议。提前非常感谢您

我尝试检查值的代码是

<?php
$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"';

}

else{

$checked = '';

}

}

}
?>

这是我的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 ?>>

【问题讨论】:

    标签: php html mysql checkbox


    【解决方案1】:
    <?php
    // Create connection
    $con=mysqli_connect("hostname","username","pass","dbname");
    
    // 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))
      {
        $focus=explode(",",$row['focus']);
    
    ?>
    <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 } ?> >
    <?php
    }
    ?>
    

    【讨论】:

    • 非常感谢您,这完全有效!我把它复杂化了。这更有意义。再次感谢您的帮助
    【解决方案2】:
    <?php
    
     $focusedValues = array();
    
    $focusQuery = mysql_query("SELECT focus FROM members_teachers WHERE id = $member_id") or die;
    
    while ($row = mysql_fetch_assoc($focusQuery)){
    
        $focusedValues = explode(',', $row['focus']);
    }
    ?>
    
    <label for="art-focus">Art</label>
                            <input name="focus[]" type="checkbox" value="Art" <?php echo in_array('Art', $checked) ?>>
    
    <label for="math-focus">Mathematics</label>
                                <input name="focus[]" type="checkbox" value="Mathematics" <?php echo in_array('Mathematics', $checked) ?>
    
    <label for="dance-focus">Dance</label>
                                <input name="focus[]" type="checkbox" value="Dance" <?php echo in_array('Dance', $checked) ?>
    

    我不知道你为什么第二次SELECTing,那没意义,你已经知道检查了什么,因为它在$focusedValues 中。此外,在您的代码中,如果未检查任何内容,$checked 将为空,否则为 checked="checked"。显然,每个输入都需要一个变量,不是吗?

    【讨论】:

      【解决方案3】:
      <?php  $hobby = $row['hobbies']; 
          $hobbies = explode (' ', $hobby);
      ?>
      <input type="checkbox" name="hobbies[]" value="cricket"  <?php echo in_array('cricket', $hobbies?'checked':'') ?> >cricket
      <input type="checkbox" name="hobbies[]" value="singing"  <?php  echo in_array('singing' , $hobbies ?'checked': '')  ; ?> >singing
      <input type="checkbox" name="hobbies[]" value="football" <?php  echo in_array('football', $hobbies  ?'checked': '') ; ?> >footballl 
      

      【讨论】:

        猜你喜欢
        • 2012-08-31
        • 2013-03-09
        • 1970-01-01
        • 2016-03-08
        • 2015-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多