dqpwdai095465 2015-07-20 12:29 采纳率: 100%
浏览 184
已采纳

MySQL更新和动态多选

I have form with a multiple selection, which has MySQL table column names as dynamic options.

<select id="rankRights" size="10" class="form-control" name="rights[]" multiple>
    <?php
        $sql = $pdo->query("SELECT * FROM ranks");
        $rights = $sql->fetch(PDO::FETCH_ASSOC);

        foreach($rights as $key => $value) {
            if($key != 'Name' || $key != 'Id') {
                echo "<option value=\"".$key."\""; 
                    if($result[$key] == '1') { 
                        echo " selected"; 
                    } 
                echo "> " . $key . "</option>";
            }
        }
    ?>
</select>

And in table there is user rights as columns and value 1 or 0 (means true or false, example below). There can be any number of columns.

.--------------------------------------------------------------------------------.
| Id   | Name       | ADMIN    | ADMIN_EDITUSER   | ADMIN_DELETEUSER   | etc     |
'--------------------------------------------------------------------------------'
| 1    | Admin      | 1        | 1                | 1                  | ...     |
| 2    | Default    | 0        | 0                | 0                  | ...     |
'--------------------------------------------------------------------------------'

What I want to do is update the values to 1 if user has selected column name in multiple selection and vice versa. How can I post those to the PDO query..

EDIT: So, if I submit the form (for example selected ADMIN and ADMIN_EDITUSER from multiple selection), I'll get POST params like rights[]=ADMIN and rights[]=ADMIN_EDITUSER -> in that case I want to update values of columns ADMIN and ADMIN_EDITUSER to 1, and values of all other columns (options not selected) to 0.

I have read following questions and thinking something, but I don't even now know how to do this.

  • 写回答

1条回答 默认 最新

  • dqdmvg7332 2015-07-20 17:32
    关注

    You could create an array of permission/column names:

    $names = array('ADMIN','ADMIN_EDITUSER',...);
    

    Initialize permissions to zero:

    $permval = array();
    foreach ($names as $name)
       $permval[$name] = 0;
    

    Walk the posted values to set related permissions:

    foreach ($_REQUEST as $key => $value)
    {
      if ( isset($permval[$key]) )
        $permval[$key] = $value;
    }
    

    This at least gives you a defined list of values that you use can use to bind to a set of parameters - one for each permission name. Ensure the list of permission names and a statement preparation are kept in sync.

    Any easier alternative may be to create a SQL statement on the fly but you may prefer to use PDO.

    NOTE: I did not attempt to execute any of the above; it may contain typos!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应