<?php 
require('db.php');
include("auth.php");
$id=$_REQUEST['id'];

$query="Select Plants.picture, Seeds.germination, Seeds.soil, Seeds.sunlight, Seeds.days_to_harvest, Planted.plant_name, Planted.id, Plants.id, Seeds.id, Planted.planted_date from Planted, Seeds, Plants WHERE Plants.id='".$id."' AND Planted.id='".$id."' AND Seeds.id='".$id."'";

$result = mysql_query($query) or die ( mysql_error());
$row = mysql_fetch_assoc($result);

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Update Record</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p><a href="dashboard.php">Dashboard</a> | <a href="insert.php">Insert New Record</a> | <a href="logout.php">Logout</a></p>
<h1>Update Record</h1>
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$id=$_REQUEST['id'];

$plant_name =$_REQUEST['Plant_name'];
$Days_to_germinate =$_REQUEST['Days_to_germinate'];
$Days_to_harverst = $_REQUEST['Days_to_harverst'];
$plant_name =$_REQUEST['Plant_name'];
$Soil =$_REQUEST['Soil'];
$Sunlight =$_REQUEST['Sunlight'];
$submittedby = $_SESSION["username"];

$update1="update Planted set plant_name='".$plant_name."' where id='".$id."'";
$update2="update Plants set plant_name='".$plant_name."', picture='".$plant_name."' where id='".$id."'";
$update3="update Seeds set plant_name='".$plant_name."', germination='".$Days_to_germinate."', days_to_harvest='".$Days_to_harverst."', soil='".$Soil."', sunlight='".$Sunlight."' where id='".$id."'";

mysql_query($update1) or die(mysql_error());
mysql_query($update2) or die(mysql_error());
mysql_query($update3) or die(mysql_error());

$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
<div>
<form name="form" method="post" action=""> 
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="Plant_name" placeholder="Enter Plant name" required value="<?php echo $row['plant_name'];?>" /></p>
<p><input type="text" name="Soil" placeholder="Enter Soil" required value="<?php echo $row['soil'];?>" /></p>
<p><input type="text" name="Sunlight" placeholder="Enter Sunlight" required value="<?php echo $row['sunlight'];?>" /></p>
<p><input type="text" name="Days_to_germinate" placeholder="Enter Days_to_germinate" required value="<?php echo $row['germination'];?>" /></p>
<p><input type="text" name="Days_to_harverst" placeholder="Enter Days_to_harverst" required value="<?php echo $row['days_to_harvest'];?>" /></p>

<p><input name="submit" type="submit" value="Update" /></p>
</form>
<?php } ?>
</div>
</div>
</body>
</html>
