<?php
// including the database connection file
include_once("db/config.php");

if(isset($_POST['update']))
{	

	$id = mysqli_real_escape_string($mysqli, $_POST['id']);
	$image = mysqli_real_escape_string($mysqli, $_FILES['image']['name']);
	$target = "images/".basename($_FILES['image']['name']);	
	
	// checking empty fields
	if(empty($image)) {	
			
		
			echo "<font color='red'>Please select image to update or <a href='index.php'>Go back</a>.</font><br/>";
	

	} else {	
		//updating the table
		$result = mysqli_query($mysqli, "UPDATE comments SET image='$image' WHERE id=$id");
		if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
	$msg = "Image uploaded successfully";
}else{
	$msg = "There was a problem uploading image";

}

		//redirectig to the display pcode. In our case, it is index.php
		header("Location: index.php");
	}
}
?>
<?php
//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM comments WHERE id=$id");

while($res = mysqli_fetch_array($result))
{
	
	$image = $res['image'];
}
?>
<html>
<head>	
	<title>Update Image</title>

	<style type="text/css">
		


tr img{
	border-radius: 100px;
	border: 1px solid red;
}

input[type=submit] {
	color: white;
	padding: 8px 20px;
	border: 1px solid red;
	border-radius: 5px;
	background: red;
}

	</style>

</head>

<body>
	<a href="index.php" style="text-align: center;">Back Home</a>
	<br/><br/>
	
	
<form name="form1" method="post" action="" enctype="multipart/form-data">
		<table border="0">
			
			<tr> 
				<td>
				<?php if($image != ""): ?>
							<img src="images/<?php echo $image; ?>" width="100px" height="100px">
							<?php else: ?>
							<img src="images/default.png" width="100px" height="100px">
							<?php endif; ?>
				</td>
			</tr>

			<tr> 
				<td><input type="file" name="image"> </td>
			</tr>

			<tr>
				<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
			</tr>

			<tr>
				<td><input type="submit" name="update" value="Update"></td>
			</tr>

		</table>
	</form>
	
</body>
</html>
