从数据库值中检查复选框 [英] Checking checkboxes from database values

查看:69
本文介绍了从数据库值中检查复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

将复选框值输入到表名members_teachers中的一个名为focus的列中,并用逗号分隔,例如艺术,数学,舞蹈等.我不确定实现目标的距离有多远感谢您可以提供的任何帮助或建议.提前非常感谢您

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

我尝试检查值的代码是

<?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
// 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
}
?>

这篇关于从数据库值中检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆