gpt4 book ai didi

Php - 变量名中的变量

转载 作者:行者123 更新时间:2023-12-01 00:45:55 25 4
gpt4 key购买 nike

我有一个带有多个复选框的表单。如果选中,这些框在 mysql 数据库中各占一行。现在,我需要一个循环来构建查询以删除所有未选中的行。我尝试了下面的一个,但数组从未填充过......感谢任何帮助...

$chk_cab_1  = isset($_POST['chk_cab_1']) ?: 0;
$chk_cab_2 = isset($_POST['chk_cab_2']) ?: 0;
$chk_cab_3 = isset($_POST['chk_cab_3']) ?: 0;
$chk_cab_4 = isset($_POST['chk_cab_4']) ?: 0;
$chk_cab_5 = isset($_POST['chk_cab_5']) ?: 0;
$chk_cab_6 = isset($_POST['chk_cab_6']) ?: 0;
$chk_cab_7 = isset($_POST['chk_cab_7']) ?: 0;
$chk_cab_8 = isset($_POST['chk_cab_8']) ?: 0;
$chk_cab_9 = isset($_POST['chk_cab_9']) ?: 0;
$chk_cab_10 = isset($_POST['chk_cab_10']) ?: 0;
$chk_cab_11 = isset($_POST['chk_cab_11']) ?: 0;


$check_cab = array();
$del_cab_ids = array();
for($i = 1; $i<=$counter; $i++) {
$check_cab["chk_cab_$i"] = $chk_cab_$i;
if($check_cab["chk_cab_$i"] == 0) {
$del_cab_ids[] = $i;
}
}

$sql = "DELETE FROM mytable
WHERE first_id = $t_key
AND second_id IN ($del_cab_ids)";

最佳答案

我会更改 HTML,如果可能的话,像这样重命名复选框:

<input name="chk_cab[0]" type="checkbox" value="1" />
...
<input name="chk_cab[5]" type="checkbox" value="1" />
...

然后反转查询:

$sql = "DELETE FROM mytable WHERE first_id = ?";

if (isset($_POST['chk_cab']) {
$ids = join(',', array_map('intval', array_keys($_POST['chk_cab'])));

$sql .= " AND second_id NOT IN ($ids)";
}

重要:这假设所有条目都匹配 first_id = <whatever>[1 .. $counter] 内.

关于Php - 变量名中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219090/

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