【问题标题】:PHP ARRAY, 2 foreach, input name=" " problemPHP ARRAY,2个foreach,输入name=""问题
【发布时间】:2020-01-12 08:35:46
【问题描述】:

好吧,我有一个大问题。 让我解释一下。

我需要列出一些输入(复选框)中的所有模式,然后我们可以选择我们想要操作的模式,以授予特定用户权限。

不管怎样,我明白了,正如你所见:

<div id="list-schemas">
    <?php
        foreach ($schemas as $elt) {
        echo '<input type="checkbox" name="schemas[]" value="' . $elt->getSchema() . '"/>' . $elt->getSchema() . '<br />';
        }
    ?>
</div>

然后,我还需要设置一些带有权限的复选框,我这样做了:

<div id="div-privileges">
    <?php
        foreach ($schemas as $elt) {
            echo '<div class="list">';
            echo '<label for="list">' . $elt->getSchema() . ' :</label><br />';
            echo '<input type="checkbox" name="privileges[]" value="REVOKE"/> REVOKE ? <br />';
            echo '<input type="checkbox" name="privileges[]" value="ALL"/> ALL PRIVILEGES ? <br />';
            echo '<hr>';
            echo '<input type="checkbox" name="privileges[]" value="SELECT"/> SELECT ? <br />';
            echo '<input type="checkbox" name="privileges[]" value="INSERT"/> INSERT ? <br />';
            echo '<input type="checkbox" name="privileges[]" value="UPDATE"/> UPDATE ?  <br />';
            echo '<input type="checkbox" name="privileges[]" value="DELETE"/> DELETE ?  <br />';
            echo '<hr>';
            echo '<input type="checkbox" name="privileges[]" value="CREATE"/>CREATE ?  <br />';
            echo '</div>';
        }
    ?>
</div>

也就是说,这是我在 UserManager.class.php 中的函数更新:

public static function update(User $newPerso){

    $db = DbConnect::getDb();
    $newLogin= pg_escape_string($newPerso->getLogin());
    $schemas=$newPerso->getSchemas();
    $privileges=$newPerso->getPrivileges();

    if (isset($schemas)){

        foreach($schemas as $schema){

            if (isset($privileges)){

                foreach($privileges as $privilege){

                    if($privilege=="REVOKE"){
                        pg_query("{$privilege} ALL ON ALL TABLES IN SCHEMA {$schema} FROM {$newLogin};");
                    }

                    else if($privilege=="CREATE"){
                        pg_query("GRANT {$privilege} ON SCHEMA {$schema} TO {$newLogin};");
                    }

                    else if($privilege=="ALL" || $privilege=="INSERT" || $privilege=="SELECT" || $privilege=="UPDATE" || $privilege=="DELETE"){
                        pg_query("GRANT {$privilege} ON ALL TABLES IN SCHEMA {$schema} TO {$newLogin};");
                    }
                }
            }
        }
    }
}

事实上,它确实有效,但是,

当我选择例如 2 个模式时(比如说“模式 1”和“模式 2”), & 当我为'schema1'选择'SELECT'和为'schema2'选择'INSERT'时,

结果:用户将在“schema1”和“schema2”中拥有“SELECT”+“INSERT”...

我真的被困住了,我真的不知道该怎么办......

【问题讨论】:

    标签: php arrays postgresql loops foreach


    【解决方案1】:

    只需将您的输入修改为

    <input type="checkbox" name="privileges[schema1][]" value="REVOKE"/> REVOKE ? <br />
    

    看到这个[schema1]了吗?它将在$_POST 数组中为您提供另一个级别。

    因此你可以像这样迭代它:

    foreach ($_POST['privileges'] as $schema => $privileges) {
        foreach ($privileges as $privilege) {
            // you know what privilege is for what schema
        }
    }
    

    【讨论】:

    • 好吧@u_mulder,非常感谢您的帮助!事实上,它有效!但我做了一些测试,我遇到了最后一个问题。如您所见,如果我这样做:image.noelshack.com/fichiers/2019/37/3/1568186255-capture.png 我的用户将拥有(在该示例中):这两种模式中的所有特权...
    • 我看不出你做了什么来得到这样的输出。因此,最好提出一个新问题并提供生成此类输出的代码摘录。
    • 是的,我会这样做的,无论如何@u_mulder 非常感谢您抽出宝贵的时间。
    猜你喜欢
    • 2011-08-04
    • 2020-11-18
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多