prabin
prabin

Reputation:

Insert array values into database

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

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

Upvotes: 1

Views: 2621

Answers (2)

thedz
thedz

Reputation: 5572

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.

Upvotes: 0

dnagirl
dnagirl

Reputation: 20456

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')";
  }

Upvotes: 2

Related Questions