<?php
 	session_start(); 

	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}



$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");
	include("connectp.php");

	
	
	
	
	$rolemid=$_POST['rolemid'];
	$smodulemid=$_POST['smodulemid'];
	$attechment="";
	
	$n=0;
	
 	 $query="delete from res_moduleallocation_detail where rolemid=$rolemid";

	$result= mysqli_query($conn, $query) or die("Error");
	
	foreach ($smodulemid as $one_by_one_sid) 
					{
					if(isset($_POST['malistview'.$one_by_one_sid]) || isset($_POST['maview'.$one_by_one_sid]) || isset($_POST['maadd'.$one_by_one_sid]) || isset($_POST['maedit'.$one_by_one_sid]) || isset($_POST['madelete'.$one_by_one_sid])  )
					{
						$malistview=0;
						$maview=0;
						$maadd=0;
						$maedit=0;
						$madelete=0;
					if($_POST['malistview'.$one_by_one_sid] == 1)
					{
						$malistview = 1;
					}
					if( $_POST['maview'.$one_by_one_sid] ==1 || ($_POST['maadd'.$one_by_one_sid] ==1) || ($_POST['maedit'.$one_by_one_sid] ==1) || ($_POST['madelete'.$one_by_one_sid] == 1)  )
					{
						$malistview = 1;
					}
					
					 if(($_POST['maview'.$one_by_one_sid] ==1))
					{
						$maview = 1;
					}
					 if(($_POST['maadd'.$one_by_one_sid] ==1))
					{
						$maadd = 1;
					}
					 if(($_POST['maedit'.$one_by_one_sid]) ==1)
					{
						$maedit = 1;
					}
					 if(($_POST['madelete'.$one_by_one_sid]) == 1)
					{
						$madelete = 1;
					}
					 $sql1="Insert into res_moduleallocation_detail (rolemid,smodulemid,malistview,maview,maadd,maedit,madelete) values (?,?,?,?,?,?,?)";
					$stmt1= $db7->prepare($sql1);
				$stmt1->execute([$rolemid,$one_by_one_sid,$malistview,$maview,$maadd,$maedit,$madelete]);
				
					}
					$n++;
					}
		
		
	
			
	$_SESSION['msg'] = "Record saved successfully";
	header('Location: moduleallocation.php');
			
	
?>