<?php
ob_start();
include('include/connect.php');
if(isset($_GET['id']))
{
$qry = "SELECT * FROM posts where id=".$_GET['id'];
$result = mysql_query($qry);
$row = mysql_fetch_array($result);

if($_SERVER["REQUEST_METHOD"] == "POST")
{
	$title = $_POST["title"];
	$sdate = $_POST["sdate"];
	$post = $_POST["post"];
	IF($_FILES['file']['name']!='')
	{
	 $file='blog-pics/'.$row['image'];
	 @unlink($file);
	 $tmp_name = $_FILES["file"]["tmp_name"];
	 $namefile = $_FILES["file"]["name"];
	 $ext = end(explode(".", $namefile));
	 $image_name=time().".".$ext;
	 $fileUpload = move_uploaded_file($tmp_name,"blog-pics/".$image_name);
	}
	else
	{
	$image_name=$row['image'];
	}
	 $sqlAdd ="update posts set title='".$title."', sdate='".$sdate."', post='".$post."', image='".$image_name."'  where id=".$_GET['id'];
	 mysql_query($sqlAdd);
	 header("Location:blog-posts-add.php?id=".@$_GET['id']."&msg=success");
	 exit;
}		
}
else
{
if($_SERVER["REQUEST_METHOD"] == "POST")
{
	$title = $_POST["title"];
	$sdate = $_POST["sdate"];
	$post = $_POST["post"];
	IF($_FILES['file']['name']!='')
	{
		$tmp_name = $_FILES["file"]["tmp_name"];
		$namefile = $_FILES["file"]["name"];
		$ext = end(explode(".", $namefile));
		$image_name=time().".".$ext;
		$fileUpload = move_uploaded_file($tmp_name,"blog-pics/".$image_name);
	}
	$sqlAdd = mysql_query("insert into posts(title,sdate,post,image) VALUES('$title','$sdate','$post','$image_name')");
header("Location:blog-posts.php?msg=success");
	exit;
}
}
ob_end_flush();
if(isset($_GET['msg']))
{
?><div style="color:red;padding-bottom:10px;" class="form-message" align="center"><b>Your post published successfully.</b></div>
<?php }?><!DOCTYPE html>
<html dir="ltr" lang="en-US"><head>
<style type="text/css" media="all">
	@import "css/info.css";
	@import "css/main.css";
	@import "css/widgEditor.css";
</style>
<script type="text/javascript" src="scripts/widgEditor.js"></script>
<meta charset="utf-8">
<title>Add,Modify & Delete Blog Posts</title>
<meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">
<link rel="stylesheet" href="css/adminstyle.css" media="screen">
<link rel="stylesheet" href="css/adminstyle.responsive.css" media="all">
<style>.content .postcontent-0 .layout-item-0 { padding-right: 10px;padding-left: 10px;  }
.ie7 .post .layout-cell {border:none !important; padding:0 !important; }
.ie6 .post .layout-cell {border:none !important; padding:0 !important; }
</style></head>
<body>
<div id="main">
<div class="header-bg clearfix">
</div>
<div id="hmenu-bg" class="bar nav clearfix">
</div>
<div class="sheet clearfix">
<header class="header clearfix">
<div class="shapes">
<h1 class="headline" data-left="0.5%">
<a  href="#" target="_blank">Blog Posts</a>
</h1>
</div>
<nav class="nav clearfix">
<div class="nav-inner">
<ul class="hmenu"><li><a href="blog-posts.php" class="active">Home</a></li></ul> 
</div>
</nav>
</header>
<div class="layout-wrapper clearfix">
<div class="content-layout">
<div class="content-layout-row">
<div class="layout-cell content clearfix"><article class="post article">
<h2 class="postheader">Add,Modify & Delete Blog Posts</h2>
<div class="postcontent postcontent-0 clearfix"><div class="content-layout">
<div class="content-layout-row">
<div class="layout-cell layout-item-0" style="width:100%">
<form  method="post" name="login" id="login" enctype="multipart/form-data">
<table class="table" width="100%">
<tr>
<td width="10%">Title</td>
<td><input name="title" type="text" placeholder="Enter Blog Title" required value="<?php echo @$row['title'];?>" maxlength="100"></td>
</tr>
<tr>
<td width="10%">Date</td>
<td><input name="sdate" type="text" required placeholder="Enter Blog Date" value="<?php echo @$row['sdate'];?>" maxlength="10"></td>
</tr>
<tr>
<td width="10%">Script</td>
<td>
<textarea rows="20" name="post" id="noise" class="widgEditor nothing" wrap='physical' required value="<?php echo @$row['post'];?>">post script here</textarea>
</td>
</tr>
<tr>
<td width="10%">Image</td>
<td><input name="file" type="file"  required ></td>
</tr>
<?php if(isset($row['image'])) 
{?><tr>
<td>&nbsp;</td>
<td><img src="blog-pics/<?php echo $row['image'];?> " height="120" width="120"></td>
</tr><?php }?><tr>
<td>&nbsp;</td>
<td>
<input name="submit" value="Submit" type="submit" class="submit">
<input name="submit" value="Cancel" type="button" class="submit" onClick="window.location='blog-posts.php'">
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
</article></div>
    </div>
</div>
</div>
</div>
<footer class="footer clearfix">
<div class="footer-inner">
<p></p>
<p class="page-footer">
</p>
</div>
</footer>
</div>
<link rel='Stylesheet' property='stylesheet' href='style.css' />

</body></html>