我有多个选择框,可以更改" about_demographics"在MySQL中。
示例:
在about_demographics
我有值:g01,g02,g03,
如果我尝试按选择框更新这些值,并选择g02,g03,g05,
并取消选择g01,
,则值为g01,g02,g03,g02,g03,g05,
我的期望:
在about_demographics
我有值:g01,g02,g03,
如果我尝试按选择框更新这些值,并选择g02,g03,g05,
并取消选择g01,
,则值为g02,g03,g05,
<select name="work[]" multiple>
<option value="g01">1</option>
<option value="g02">2</option>
<option value="g03">3</option>
<option value="g04">4</option>
<option value="g05">5</option>
</select>
<?php
$work_array = array('g01', 'g02', 'g03', 'g04', 'g05');
foreach ($work_array as $a_work) {
if (in_array($a_work, $_POST['work'])) {
$first_part_all.=$a_work.",";
}
}
$db->Query("UPDATE user_about SET about_demographics = CONCAT(about_demographics,
'$first_part_all') WHERE id = '".$data['id']."'");
?>
答案 0 :(得分:1)
我很确定这是因为您要附加预先存在的$first_part_all
变量而不删除以前的值。您应该能够通过使用implode()
<select name="work[]" multiple>
<option value="g01">1</option>
<option value="g02">2</option>
<option value="g03">3</option>
<option value="g04">4</option>
<option value="g05">5</option>
</select>
<?php
$db->Query("UPDATE user_about SET about_demographics = CONCAT(about_demographics,
'".implode(",",$_POST['work'])."') WHERE id = '".$data['id']."'");
?>
编辑:此替代方案仍然需要循环,但它会在开头清除您的$first_part_all
变量,并且几乎不需要对现有代码进行更改以产生预期结果:
<select name="work[]" multiple>
<option value="g01">1</option>
<option value="g02">2</option>
<option value="g03">3</option>
<option value="g04">4</option>
<option value="g05">5</option>
</select>
<?php
$work_array = array('g01', 'g02', 'g03', 'g04', 'g05');
$first_part_all = '';
foreach ($work_array as $a_work) {
if (in_array($a_work, $_POST['work'])) {
$first_part_all.=$a_work.",";
}
}
$db->Query("UPDATE user_about SET about_demographics = CONCAT(about_demographics,
'$first_part_all') WHERE id = '".$data['id']."'");
?>