A
A
Alexander Tokmakov2014-08-31 07:59:25
PHP
Alexander Tokmakov, 2014-08-31 07:59:25

How to populate checkboxes from a request?

Hello. Read a lot about the differentiation of rights. Many solutions are inefficient, many are not clear.
Implemented his own, for the project. The bottom line is this: there is a role table, which lists the roles:
1. admin
2. Client 3.
Manager
4. Master, etc.
There is a right table, where the rights are:
1. access to the
admin panel 2. adding applications
3. editing applications
4. adding users
Well, the third access is for linking these two tables.
In the right place I write:

<?$right='4'; include('access.php');if(isset($access['id'])){
echo "доступно только если у пользователя с ролью админ есть право 4"; }?>

Checking access rights in the access.php file:
<?php
//проверка прав доступа
$dostup = mysqli_query($db, "SELECT id FROM access WHERE id_right = $right AND id_role = $user[role] ");
$access = mysqli_fetch_assoc($dostup);

I don't know how elegant this solution is, but it works great. I create a role, give it rights, and voila.
But I ran into a problem for which I can not find a simple solution.
The task is this:
On the page where the rights are distributed, there are checkbox's 0de7eaaaaf1c41f286647180e00de6c3.JPG
when I select the rights of the admin or manager, the checkbox's should be filled. Therefore, when changing the checkboxes, they must be written to the database.
Now, what am I doing. When I choose to edit the role, a modal window opens with a request and checkboxes:
$access1 =  mysqli_query($db,"SELECT id_right FROM access WHERE id_role = '$_GET[id]'");
      if($ass = mysqli_fetch_array($access1)){
          do{
              print_r($ass['id_right']);

//Скрипт выдает 1,2,3 или 4 и т.д. В зависимости от выбора $_GET[id]
          }while($ass = mysqli_fetch_array($access1));
      }

and checkboxes below:
<div class="checkbox"><label><input type="checkbox" name="dostup-3" value="on"  /> Доступ в административную панель</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-1" value="on"  /> Добавление и редактирование заявок</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-2" value="on"  /> Удаление заявок</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-4" value="on"  /> Быстрое обслуживание</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-5" value="on"  /> Управление ролямя</label></div>

Question: How do I fill in the checkboxes from the request above? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Snewer, 2014-08-31
@calliko

First, a few words on security and performance:
SQL injection. A lot has been written on this topic, check all the data from outside.
works slower than:
I didn’t quite understand about checkboxes. If you need the checkbox to become active when some condition is triggered, then:

$ac = array();
$access1 =  mysqli_query($db,"SELECT id_right FROM access WHERE id_role = '$_GET[id]'");
      if($ass = mysqli_fetch_array($access1)){
          do{
            $ac[] =   $ass['id_right'];
          }while($ass = mysqli_fetch_array($access1));
      }

<div class="checkbox"><label><input type="checkbox" name="dostup-3" value="on" <?php
echo ( in_array(1, $ac) ) ? 'checked' : '' ;
?> /> Доступ в административную панель</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-1" value="on"  <?php
echo ( in_array(2, $ac) ) ? 'checked' : '' ;
?> /> Добавление и редактирование заявок</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-2" value="on"  <?php
echo ( in_array(3, $ac) ) ? 'checked' : '' ;
?> /> Удаление заявок</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-4" value="on"  <?php
echo ( in_array(4, $ac) ) ? 'checked' : '' ;
?> /> Быстрое обслуживание</label></div>
<div class="checkbox"><label><input type="checkbox" name="dostup-5" value="on"  /> Управление ролямя</label></div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question