<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:checkLogin.php');
}

$userArray = 0;

$userId = isset($_REQUEST['userId']) ? $_REQUEST['userId'] : 0;

if(isset($_POST['submitButton']))
{
	$userName = isset($_POST['userName']) && $_POST['userName'] != '' ? $_POST['userName'] : '';
	$password = isset($_POST['password']) && $_POST['password'] != '' ? $_POST['password'] : '';
	$userType = isset($_POST['userType']) && $_POST['userType'] != '' ? $_POST['userType'] : '';
	if($userId == 0)
	{
		$insUserQry = "INSERT INTO user(userName,password,userType)
		               VALUES ('".$userName."','".$password."','".$userType."')";
    $insUserQryRes = mysql_query($insUserQry);
    if(!$insUserQryRes)
    {
    	echo "Data Not Inserted : ".mysql_error();
    }
    else
    {
    	header('Location:userList.php');
    }
	}
	else
	{
	  $updateUserQry = "UPDATE user
	                       SET userName = '".$userName."',
	                           password = '".$password."',
	                           userType = '".$userType."'
	                     WHERE userId = ".$userId;
    $updateUserQryRes = mysql_query($updateUserQry);
    if(!$updateUserQryRes)
    {
    	echo "Data Not Update : ".mysql_error();
    }
    else
    {
    	header('Location:userList.php');
    }
	}
}
$selUserQry = "SELECT userId,userName,password,userType
                 FROM user
                WHERE userId = ".$userId;
$selUserQryRes = mysql_query($selUserQry);
if($dataRow = mysql_fetch_array($selUserQryRes))
{
	$userArray = array();
	$userArray['userName'] = $dataRow['userName'];
	$userArray['password'] = $dataRow['password'];
	$userArray['userType'] = $dataRow['userType'];
}
$userTypeArray[0] = 'Admin';
$userTypeArray[1] = 'other';

include("bottom.php");
$smarty->assign('userArray',$userArray);  
$smarty->assign('userTypeArray',$userTypeArray);  
$smarty->assign('userId',$userId);  
$smarty->display('userEntry.tpl');  
?>