1

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

复选框值输入到表名 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 ?>>
4

3 回答 3

4
<?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
}
?>
于 2013-08-27T18:36:26.187 回答
0
<?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) ?>

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

于 2013-08-27T18:16:34.410 回答
0
<?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 
于 2016-06-24T12:27:15.820 回答