简体   繁体   English

php-变量名中的变量

[英]Php - variable in variable name

I have a form with several checkboxes. 我有一个带有多个复选框的表格。 These boxes have one row each in a mysql db if checked. 如果选中,这些框在mysql数据库中每个都有一行。 Now, I need to have a loop that build a query to delete all rows that is not checked. 现在,我需要一个循环来构建查询以删除所有未选中的行。 I tried the one below but the array is never populated... Any help is appreciated... 我尝试了下面的方法,但是从未填充该数组。

$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)";

I would change the HTML, if possible to rename the checkboxes like this: 我将更改HTML,如果可能的话,请重命名以下复选框:

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

And then invert the query: 然后反转查询:

$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)";
}

Important : This assumes that all entries matched by first_id = <whatever> are accounted for within [1 .. $counter] . 重要说明 :假定所有与first_id = <whatever>匹配的条目都在[1 .. $counter]

Create an array at the start . 在开始时创建一个数组。 This avoids nonsense with "variable variables" 1 这避免了“变量变量”的废话1

$del_cab_ids = array();
for($i = 1; $i<=$counter; $i++) {
   # it's easier/cleaner to use a dynamic key than a dynamic variable
   $v = isset($_POST['chk_cab_' . $i]) ?: 0;
   if ($v == 0) {
     $del_cab_ids[] = $i;
   }
}

Also, at least use mysqli_real_escape_string .. but I recommend using placeholders and binding the values .. 另外, 至少使用mysqli_real_escape_string ..但我建议使用占位符并绑定值..


1 While I don't agree with "variable variables", the usage is as such: 1虽然我不同意“变量变量”,但用法是这样的:

$varname = "chk_cab_" . $i;
$value = $$varname;

# or
$value = ${"chk_cab_" . $i}

尝试这个....

$check_cab["chk_cab_$i"] = ${'chk_cab_'.$i}

The complete structure seems to be a bit ... ugly. 完整的结构似乎有点...难看。 Sorry for that. 抱歉 ;) So you can avoid that mass of unnecessary variables by using a simple array for your post data. ;)因此,您可以通过为帖子数据使用简单的数组来避免大量不必要的变量。

<input type="checkbox" value="1" name="my-checkbox[]" id="my-checkbox-1" /> 1
<input type="checkbox" value="2" name="my-checkbox[]" id="my-checkbox-2" /> 2

With this you can grab all the IDs in one simple line of PHP code as follows: 有了这个,您可以在一个简单的PHP代码行中获取所有ID,如下所示:

if (isset($_POST['my-checkbox'])) $myIDs = array_map('intval', $_POST['my-checkbox']);

So all your post IDs have been validated as an integer value so you can proceed with your database statement. 因此,您所有的帖子ID均已被验证为整数值,因此您可以继续执行数据库语句。 For this I recommend using statements like the PDO object gives you with PDO Statements to avoid security issues. 为此,我建议使用PDO对象之类的语句为您提供PDO语句,以避免安全问题。 Have a look at the following example: 看下面的例子:

try {
    $pdo = new PDO(...);
    $sql = "DELETE FROM mytable WHERE first_id = :first_id AND FIND_IN_SET(CAST(second_id AS char), :second_id)";

    $statement = $pdo->prepare($sql);
    $statement->execute(array(
        ':first_id' => $first_id,
        ':second_id' => implode(",", $myIDs)
    ));
} catch(PDOException $e) {
    // error handling
}

That 's all. 就这样。 With this small example all your structure and security problems should have been solved. 通过这个小例子,您所有的结构和安全问题都应该得到解决。

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM