简体   繁体   中英

Php - variable in variable name

I have a form with several checkboxes. These boxes have one row each in a mysql db if checked. 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:

<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] .

Create an array at the start . This avoids nonsense with "variable variables" 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 ..


1 While I don't agree with "variable variables", the usage is as such:

$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:

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. For this I recommend using statements like the PDO object gives you with PDO Statements to avoid security issues. 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)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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