Alejo_Blue
Alejo_Blue

Reputation: 615

Updating database from an array checkbox dynamic

How can I update database from a form with different checkbox and status? I have two forms: -1 for inserting menus to assign to a user and.. -2 for updating the menus already asigned to this user. I have implented the form to assign different menus to a user and it works just fine (see form1_1 and form1_2 code) But my problem comes when i want to update/change the menus that i've already assign to this user and assign another menus to the user (see form2 code)

Form1_1 code: (This is part of the form that helps me assign menus to a user)

    <td>Menus a asignar:</td>
    <td>
    <?php
    $query_menus_checkbox = mysql_query("SELECT id_menu, nombre_menu FROM   menus");
    while ($checkbox_mostrar = mysql_fetch_row($query_menus_checkbox)) {
    # code...
    ?>
    <input type="checkbox" name="menus[<?php echo $checkbox_mostrar[0]; ?>]"    value="<?php echo $checkbox_mostrar[0] ?>"><?php echo $checkbox_mostrar[1] ?> 
   <p></p>
   <?php
   }
   ?>
   </td>    

Form1_2 This is the code that gets the menus from the form and insert 'em on my DB:

    $res=mysql_query("select id_menu from menus");
    $arid=array();
    while($xd=mysql_fetch_assoc($res)){
    $arid[]=$xd['id_menu'];
    }

    for($i=0;$i<count($arid);$i++){
    $id_menu=$arid[$i];
    $activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'] [$id_menu]!="")?1:0;
    $inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu,  id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')"); 
    }       

This code works perfect as expected, it inserts the menus assigned to the user, but my problem is this: when updating the menus assigned to the user. This is what it doesn't work FORM2:

   <td>
    <?php 
    $query_menus_habilitados_user = mysql_query("SELECT a.nombre_menu,  b.estado, a.id_menu FROM menus_usuarios b join menus a ON a.id_menu = b.id_menu  WHERE b.id_usuario = '$id_usuario'");
    while ($checkbox_habilitados =  mysql_fetch_row($query_menus_habilitados_user)) {
    # code...
    if ($checkbox_habilitados[1] == 1) {
    # code...
    ?>
    <input type="checkbox" name="menus<?php echo $checkbox_habilitados[2] ?>" value="<?php echo $checkbox_habilitados[2] ?>" checked="checked"> <?php echo $checkbox_habilitados[0] ?>                                      
    <?php
    }else{
    ?>
    <input type="checkbox" name="menus<?php echo $checkbox_habilitados[2] ?>" value="<?php echo $checkbox_habilitados[2] ?>"> <?php echo $checkbox_habilitados[0] ?>
    <?php
    }
    }
 ?>
</td>

...And this is the code that handles the updates from the code above FORM2_:

  $res=mysql_query("SELECT id_menu FROM menus");
  $arid=array();
  while($xd=mysql_fetch_assoc($res)){
  $arid[]=$xd['id_menu'];
 }

  for($i=0;$i<count($arid);$i++){
  $id_menu=$arid[$i];
  $activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'][$id_menu]!="")?1:0;
  $inserta_menus = mysql_query("UPDATE menus_usuarios SET id_menu = '$id_menu', id_usuario = '$id', estado = '$activo' WHERE id_usuario = '$id'"); 
 }

If I use this code it only updates menus with 0's and assigns the last menu to each user, like this: menu1->8 active->0; menu2->8 active->0; menu3->8 active->0 and so on.

Upvotes: 0

Views: 48

Answers (1)

C&#233;sar Araujo
C&#233;sar Araujo

Reputation: 26

I think you should try modifiy your update sentence

"UPDATE menus_usuarios SET id_menu = '$id_menu', id_usuario = '$id', estado = '$activo' WHERE id_usuario = '$id'"

since if you do that all the menus that were set for the user will change, instead of modify the one you want, also you won't modify a foreing key for your menu, you only should change the status of it, i bet your status field is "estado".

So you could try the next sentence

UPDATE menus_usuarios SET id_usuario = '$id', estado = '$activo' WHERE id_usuario = '$id' AND id_menu = '$id_menu'

Try this sentence and let me know if it works for you.

Greets

Upvotes: 1

Related Questions