<?php session_start();
//Fetching Values from URL


require_once __DIR__."/allfunctions.php";
$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());



if(isset($_SESSION["proditemarrayinventory"]))
{
    $itemarrayprod = $_SESSION["proditemarrayinventory"];
}
else
{
 $itemarrayprod = array();
}

if(isset($_SESSION["proditemqty"]))
{
  $itemarrayquantity = $_SESSION["proditemqty"];
}
else
{
  $itemarrayquantity = array();
}


$materialdata = "";
$ctr2 = count($itemarrayprod);
//echo $itemarray;



	if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["FIRST_NAME"]))
	{
	   $itemarray = array();
	   $memberid = "";
	  $type = "";
	  $fname = "";
	}
	else
	{
	  $memberid = $_SESSION['MEMBER_ID'];
	  $type = $_SESSION['TYPE'];
	  $fname = $_SESSION['FIRST_NAME'];
	}

$username = $_POST['username'];
$password = $_POST['password'];
$agentID = $_POST['agentID'];
$userid = $_POST['userid'];
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$birthday = $_POST['birthday'];
//$address = $_POST['address'];
$address = mysqli_real_escape_string($con, $_POST['address']);
$country = mysqli_real_escape_string($con, $_POST['country']);
$region = mysqli_real_escape_string($con, $_POST['region']);
$dateToday = date('Y-m-d');
$status = "Added";
$city = mysqli_real_escape_string($con, $_POST['city']);
//$mobile = $_POST['mobile'];
$mobile = mysqli_real_escape_string($con, $_POST['mobile']);
$gender = $_POST['gender'];
$email = mysqli_real_escape_string($con, $_POST['email']);
$refID = $_POST['refID'];
$other = mysqli_real_escape_string($con, $_POST['other']);
$parent_name = getRecord('level','test','id ='.$refID);
//echo $parent_name;
//echo $refID;
//$ref_id = getRecord('id','test','name= "'.$refID.'"');
$parentlevel =  (int) $parent_name;
//echo "AND";
$level = $parentlevel + 1;
//echo $level;
$insert_sql = "UPDATE agent SET firstname='".$firstname."',middlename='".$middlename."',lastname='".$lastname."',gender='".$gender."',birthday='".$birthday."',address='".$address."',country='".$country."',city='".$city."',region='".$region."',mobile='".$mobile."',email='".$email."',refID='".$refID."',remarks='".$other."' WHERE id=".$agentID;
//$insert_sql = "INSERT INTO agent(id,firstname,middlename,lastname,gender,birthday,address,country,region,city,mobile,email,refID,remarks,datecreated,status)
//VALUES(NULL,'".$firstname."','".$middlename."', '".$lastname."','".$gender."','".$birthday."','".$address."','".$country."','".$region."','".$city."','".$mobile."','".$email."','".$refID."','".$other."','".$dateToday."','".$status."')";



		if(mysqli_query($con, $insert_sql) or die("database error: ". mysqli_error($con)))
		{
      $insert_sql2 = "UPDATE users SET FIRST_NAME='".$firstname."',LAST_NAME='".$lastname."',GENDER='".$gender."',USERNAME='".$username."',PASSWORD='".$password."' WHERE id=".$userid;
      if(mysqli_query($con, $insert_sql2) or die("database error: ". mysqli_error($con)))
      {

            echo 1;
      }



		}
		else
		{
			echo 0;
		}

?>
