<?php
session_start();
require("config.php");

if(!isset($_SESSION['auser'])){
header("location:index.php");
exit;
}

$error="";
$msg="";

$sid=0;
if(isset($_GET['id']) && is_numeric($_GET['id'])){
$sid=(int)$_GET['id'];
}else{
header("location:stateadd.php");
exit;
}

if(isset($_POST['insert'])){
$ustate=trim($_POST['ustate']);

if($ustate!=""){
$stmt=$con->prepare("UPDATE state SET sname=? WHERE sid=?");
$stmt->bind_param("si",$ustate,$sid);
if($stmt->execute()){
$msg="<p class='alert alert-success'>State Updated</p>";
header("Location:stateadd.php?msg=$msg");
exit;
}else{
$msg="<p class='alert alert-warning'>State Not Updated</p>";
header("Location:stateadd.php?msg=$msg");
exit;
}
}else{
$error="<p class='alert alert-warning'>Please fill the field</p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<title>Clan Homes</title>
<link rel="shortcut icon" type="image/x-icon" href="assets/img/favicon.png">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/feathericon.min.css">
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>

<?php include("header.php");?>

<div class="page-wrapper">
<div class="content container-fluid">

<div class="page-header">
<div class="row">
<div class="col">
<h3 class="page-title">State</h3>
<ul class="breadcrumb">
<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
<li class="breadcrumb-item active">State</li>
</ul>
</div>
</div>
</div>

<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h1 class="card-title">Edit State</h1>
<?php echo $error; ?>
</div>

<?php
$stmt=$con->prepare("SELECT sname FROM state WHERE sid=?");
$stmt->bind_param("i",$sid);
$stmt->execute();
$stmt->bind_result($sname);
if($stmt->fetch()){
?>

<form method="post">
<div class="card-body">
<div class="row">
<div class="col-xl-6">
<h5 class="card-title">State Details</h5>
<div class="form-group row">
<label class="col-lg-3 col-form-label">State Name</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="ustate" value="<?php echo htmlspecialchars($sname); ?>" required>
</div>
</div>
</div>
</div>
<div class="text-left">
<input type="submit" class="btn btn-primary" value="Submit" name="insert" style="margin-left:200px;">
</div>
</div>
</form>

<?php }else{ ?>
<p class="alert alert-danger">Invalid State ID</p>
<?php } ?>

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

</div>
</div>

<script src="assets/js/jquery-3.2.1.min.js"></script>
<script src="assets/js/popper.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="assets/js/script.js"></script>

</body>
</html>
