views:

539

answers:

2

I have a form that lists module id and also sub_module_id, for example

  • ADMIN === is parent module ID
  • users=== sub module id

here admin appears in the menu and users inside admin

now inside the form i have used checkbox like

[]Admin
    []Users

for parent module ID admin

<input  id='module_permission[]' onclick=\"selectall()\" type='checkbox' value='".$key."' name='module_permission[]'  $val_checked ><b>".$val."</b><br> ";

for sub modules

<input type='checkbox'      id='sub_module_permission[$l][]' name='sub_module_permission[$l][]' value='".$key1 ."' onclick=\"selectParent($l);\" $val_checked>".$val1."<br> ";

when i click in check box its id get post and i need to insert to databse but i am unabale to insert the sub _module id in database

to post

$module_id=$_post[module_permission]
foreach($module_id as $key=>$value){
      $sql2 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id) values
         (PERMISSION_ID_seq.nextval,'$employee_cd','$value')";
      $this->db->db_query($sql2);
      }

for sub _modules

$sub_module_id =$_POST['sub_module_permission'];
      print_r($sub_module_id);

      foreach($sub_module_id as $sub_key=>$sub_value)
      {
      echo $sub_value[1];

      $sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id) values
        (PERMISSION_ID_seq.nextval,'$employee_cd','$sub_value')";

HERE parent module id value get inserted in database but not the sub_module

please help

+2  A: 

so what prints out when you print_r($sub_module_id); and echo $sub_value[1];?

And what does your query string look like after the vars are subbed in? echo $sql4;

Try running the the query string directly in SQL rather than through PHP. This will let you know if you have a SQL error or a PHP error. If the SQL is fine, add some error checking to your PHP so you can see where it fails. Usually I wrap all queries in something like:

if(!$result=$mysqli->query($query)) 
  throw new Exception($query. " " .$mysqli->error);

From your comments and the now properly formatted code, it's possible to see that you are not referring to the 2nd dimension of your submoduleid. In otherwords you are trying to sub an array into your SQL statement.

This is what you have:

$sub_module_id =$_POST['sub_module_permission'];  //a 2d array              
  print_r($sub_module_id);                
  foreach($sub_module_id as $sub_key=>$sub_value){
    echo $sub_value[1];                
    $sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id)  
              values(PERMISSION_ID_seq.nextval,'$employee_cd','$sub_value')";
  }

And this is what you need:

$sub_module_id =$_POST['sub_module_permission'];  //a 2d array              
  print_r($sub_module_id);                
  foreach($sub_module_id as $sub_key=>$sub_value_arr){ //values are an array not a scalar
    echo $sub_value_arr[1]; 
    $sub_value= $sub_value_arr[1]; //get scalar for SQL              
    $sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id)  
              values(PERMISSION_ID_seq.nextval,'$employee_cd','$sub_value')";
  }
dnagirl
as i have declared double array in name='sub_module_permission[$l][]when i print print_r($sub_module_id) it printsArray( [2] => Array ( [0] => 9 [1] => 11 [2] => 12 [3] => 13 ) [4] => Array ( [0] => 10 [1] => 17 [2] => 18 [3] => 88 ))now i need to insert ,9,11,12...in each row in my databse , where 9,11,12 are sub modules please help as soon as possible
Thanks for your answer i have been able to solve it$sub_module_id =$_POST['sub_module_permission']; //print_r($sub_module_id); foreach($sub_module_id as $sub_key=>$sub_value){ foreach($sub_value as $s_sub_key=>$s_sub_value){ $sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id) values (PERMISSION_ID_seq.nextval,'$employee_cd','$s_sub_value')"; $this->db->db_query($sql4);using the baove code Thanx
A: 

Unrelated, but if you're getting that input from a form (and really, even if you're not), it's always a good idea to sanitize your SQL.

thedz