gpt4 book ai didi

php - 检查数据库值的复选框

转载 作者:行者123 更新时间:2023-11-29 05:27:30 24 4
gpt4 key购买 nike

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

复选框值被输入到表名 members_teachers 中的一个名为 focus 的列中,并用逗号分隔,例如 art、mathematics、dance 等 我不确定我离实现目标有多近或多远,但我非常感谢您提供的任何帮助或建议。非常感谢您提前

我尝试检查值的代码是

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

关于php - 检查数据库值的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18472510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com