<?php include("session_controller.php"); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  //$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
//format date function
function dateformatReverse($datetime){
$received_timestamp = "";
 if($datetime != ""){
 $full_date = explode(" ", $datetime);
 $date = explode("-", $full_date[0]);
 $d = $date[2];
 $mo = $date[1];
 $y = $date[0];
 $time = explode(":",$full_date[1]);
 $h = $time[0];
 $min = $time[1];
 $s = $time[2];
 
 $received_timestamp = $d."/".$mo."/".$y." ".$h.":".$min.":".$s;
 }
 return $received_timestamp;
 }
 
function dateformat($datetime){
$received_timestamp = "";
 if($datetime != ""){
 $full_date = explode(" ", $datetime);
 $date = explode("/", $full_date[0]);
 $d = $date[0];
 $mo = $date[1];
 $y = $date[2];
 $time = explode(":",$full_date[1]);
 $h = $time[0];
 $min = $time[1];
 $s = $time[2];
 
 $received_timestamp = $y."-".$mo."-".$d." ".$h.":".$min.":".$s;
 }
 return $received_timestamp;
 }
 
 $colname_Recordset1 = "-1";

 

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


mysqli_select_db($conn, $database_conn);    

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

/*echo $_POST['email'];
  echo  $_POST['phone'];
  echo $_POST['name'];
  echo $_POST['role'];
  echo $_POST['user_no'];
*/

 $updateSQL = sprintf("UPDATE admin SET name =  %s, email =  %s, phone =  %s, location =  %s, username =  %s, role =  %s WHERE s_no = %s",
   GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['phone'], "text"),
					   GetSQLValueString($_POST['location'], "text"),
					  GetSQLValueString($_POST['username'], "text"),
 
                       GetSQLValueString(($_POST['role']), "text"),
                        GetSQLValueString(($_GET['user_no']), "int"));

  $Result1 = mysqli_query($conn, $updateSQL) or die(mysqli_error($conn));
}
        
  
if($Result1){

  $updateGoTo = "users.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
  
}

if(isset($_GET['user_no']) && ($_GET['user_no'] != "") ){

  $s_no = $_GET['user_no'];

$query_accounts = "SELECT * FROM admin WHERE s_no = '$s_no' ORDER BY s_no ASC";

$_SESSION['query'] = $query_accounts; //add to session variable query for exporting to excel in download php file

$accounts = mysqli_query($conn, $query_accounts) or die(mysqli_error($conn));
$row_accounts = mysqli_fetch_assoc($accounts);
$totalRows_accounts = mysqli_num_rows($accounts);
}

?>


<html>
<head>
<title> Create User</title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<body>
<?php include("links.php");?>
<div class="container">
<div class="row">
<div class="col-sm-6">

<div style="font-family:Century Gothic; font-size:18px; font-weight:bold">Edit User</div>



<form method="post" name="form1" action="<?php echo $editFormAction; ?>" class="form-group">
  <div class="table-responsive">	
	<br/>
 <div style="font-family:Century Gothic; font-size:15px;"> Name</div>
  <input type="textfield" name="name" value="<?php echo $row_accounts['name']; ?>" class="form-control">
   
   <br/>
   <div style="font-family:Century Gothic; font-size:15px;">Email:</div>
     <input type="textfield" name="email" value="<?php echo $row_accounts['email']; ?>" class="form-control">
     <!-- <input type="text" name="sex" value="" size="32" /> -->
     <!-- <select name="tranx_type" class="form-control">
							  <option selected="selected" value="">Select</option>
							  <option value="credit" >Credit</option>
							  <option value="debit" >Debit</option>
						  
      </select>
    -->
     <br/>
      <div style="font-family:Century Gothic; font-size:15px;">Location:</div>
  <select name="location" class="form-control">
        <option selected="selected" value="" >Select State</option>
        <option value="Abia"  <?php if( strtolower($row_accounts['location']) == "abia" ) echo "selected=true"; ?>>Abia</option>
        <option value="Adamawa" <?php if( strtolower($row_accounts['location']) == "adamawa" ) echo "selected=true"; ?> >Adamawa</option>
        <option value="Akwa Ibom" <?php if( strtolower($row_accounts['location']) == "akwa ibom" ) echo "selected=true"; ?>>Akwa Ibom</option>
        <option value="Anambra" <?php if( strtolower($row_accounts['location']) == "anambra" ) echo "selected=true"; ?>>Anambra</option>
        <option value="Bauchi"  <?php if( strtolower($row_accounts['location']) == "bauchi" ) echo "selected=true"; ?>>Bauchi</option>
        <option value="Bayelsa" <?php if( strtolower($row_accounts['location']) == "bayelsa" ) echo "selected=true"; ?>>Bayelsa</option>
        <option value="Benue" <?php if( strtolower($row_accounts['location']) == "benue" ) echo "selected=true"; ?>>Benue</option>
        <option value="Borno" <?php if( strtolower($row_accounts['location']) == "borno" ) echo "selected=true"; ?>>Borno</option>
        <option value="Cross River" <?php if( strtolower($row_accounts['location']) == "cross river" ) echo "selected=true"; ?>>Cross River</option>
        <option value="Delta" <?php if( strtolower($row_accounts['location']) == "delta" ) echo "selected=true"; ?>>Delta</option>
        <option value="Ebonyi" <?php if( strtolower($row_accounts['location']) == "ebonyi" ) echo "selected=true"; ?>>Ebonyi</option>
        <option value="Edo" <?php if( strtolower($row_accounts['location']) == "edo" ) echo "selected=true"; ?>>Edo</option>
        <option value="Ekiti" <?php if( strtolower($row_accounts['location']) == "ekiti" ) echo "selected=true"; ?>>Ekiti</option>
        <option value="Enugu" <?php if( strtolower($row_accounts['location']) == "enugu" ) echo "selected=true"; ?>>Enugu</option>
        <option value="FCT" <?php if( strtolower($row_accounts['location']) == "fct" ) echo "selected=true"; ?>>FCT</option>
        <option value="Gombe" <?php if( strtolower($row_accounts['location']) == "gombe" ) echo "selected=true"; ?>>Gombe</option>
        <option value="Imo" <?php if( strtolower($row_accounts['location']) == "imo" ) echo "selected=true"; ?>>Imo</option>
        <option value="Jigawa" <?php if( strtolower($row_accounts['location']) == "jigawa" ) echo "selected=true"; ?>>Jigawa</option>
        <option value="Kaduna" <?php if( strtolower($row_accounts['location']) == "kaduna" ) echo "selected=true"; ?>>Kaduna</option>
        <option value="Kano" <?php if( strtolower($row_accounts['location']) == "kano" ) echo "selected=true"; ?>>Kano</option>
        <option value="Katsina" <?php if( strtolower($row_accounts['location']) == "katsina" ) echo "selected=true"; ?>>Katsina</option>
        <option value="Kebbi" <?php if( strtolower($row_accounts['location']) == "kebbi" ) echo "selected=true"; ?>>Kebbi</option>
        <option value="Kogi" <?php if( strtolower($row_accounts['location']) == "kogi" ) echo "selected=true"; ?>>Kogi</option>
        <option value="Kwara" <?php if( strtolower($row_accounts['location']) == "kwara" ) echo "selected=true"; ?>>Kwara</option>
        <option value="Lagos" <?php if( strtolower($row_accounts['location']) == "lagos" ) echo "selected=true"; ?>>Lagos</option>
        <option value="Nasarawa" <?php if( strtolower($row_accounts['location']) == "nasarawa" ) echo "selected=true"; ?>>Nasarawa</option>
        <option value="Niger" <?php if( strtolower($row_accounts['location']) == "niger" ) echo "selected=true"; ?>>Niger</option>
        <option value="Ogun" <?php if( strtolower($row_accounts['location']) == "ogun" ) echo "selected=true"; ?>>Ogun</option>
        <option value="Ondo" <?php if( strtolower($row_accounts['location']) == "ondo" ) echo "selected=true"; ?>>Ondo</option>
        <option value="Osun" <?php if( strtolower($row_accounts['location']) == "osun" ) echo "selected=true"; ?>>Osun</option>
        <option value="Oyo" <?php if( strtolower($row_accounts['location']) == "oyo" ) echo "selected=true"; ?>>Oyo</option>
        <option value="Plateau" <?php if( strtolower($row_accounts['location']) == "plateau" ) echo "selected=true"; ?>>Plateau</option>
        <option value="Rivers" <?php if( strtolower($row_accounts['location']) == "rivers" ) echo "selected=true"; ?>>Rivers</option>
        <option value="Sokoto" <?php if( strtolower($row_accounts['location']) == "sokoto" ) echo "selected=true"; ?>>Sokoto</option>
        <option value="Taraba" <?php if( strtolower($row_accounts['location']) == "taraba" ) echo "selected=true"; ?>>Taraba</option>
        <option value="Yobe" <?php if( strtolower($row_accounts['location']) == "yobe" ) echo "selected=true"; ?>>Yobe</option>
        <option value="Zamfara" <?php if( strtolower($row_accounts['location']) == "zamfara" ) echo "selected=true"; ?>>Zamfara</option>
            </select>
      <br/>
      <div style="font-family:Century Gothic; font-size:15px;">Phone:</div>
   <input type="text" name="phone" value="<?php echo $row_accounts['phone']; ?>" class="form-control"> 
    
  <br/>
 <div style="font-family:Century Gothic; font-size:15px;">Username:</div>
  <input type="text" name="username" value="<?php echo $row_accounts['username']; ?>" class="form-control">
  <br/>
  

  
<div style="font-family:Century Gothic; font-size:15px;">Role:</div>
 <select name="role" class="form-control">
               
                <option value="user" <?php  if($row_accounts['role'] =="user"){ echo 'selected="selected"' ; }  ?> >User</option>
                <option value="admin" <?php if($row_accounts['role'] =="admin"){ echo 'selected="selected"' ; }  ?>>Admin</option>
              
      </select>	
        
        <br/><br/>				 
	<input type="submit" value="SAVE" class="btn btn-primary" style="width:40%; height:auto;padding-left:10px;padding-left:10px; background-color:#f2f1f3; border-radius:5px; color:#333"/> &nbsp; &nbsp; &nbsp; &nbsp; <a href="assets.php" class="btn btn-primary" style="width:40%; height:auto;padding-left:10px;padding-left:10px; background-color:#f2f1f3; border-radius:5px; color:#333">CANCEL </a></td>
   
  </div>

  <input type="hidden" name="MM_update" value="form1">


</form>

  </form></div></div></div>

<br />
 </body>
 
 </html>

