<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LEIGH&amp;CO</title>
<link href="../../../css/bootstrap-4.4.1.css" rel="stylesheet">
<link href="../../../css/leighco.css" rel="stylesheet">
</head>
<body>
<p>
<center><img src="../../../img/leighco.png"/></center>
<p>
<h4>TIMs - Terrible Inventory Management System </h4>
<p>
<form method="post">
<table width="100%">
<?php

// This is the worlds worst inventory tracking system

$conn = new mysqli('db1.leigh-co.com', 'inv', 'gAXzQ3ce7QCTBVUb','lc') or die($conn->error);

// Are we posting? 
if(!empty($_POST)) {
  $label = $_REQUEST['label'];
  $loc = $_REQUEST['loc'];
  $eit = $_REQUEST['eit'];
  $sql_query = $conn->prepare("INSERT INTO item_t (eit,label,loc,ctime) VALUES (?,?,?,now()) ON DUPLICATE KEY UPDATE label = ?, loc = ?, mtime=now()") or die($conn->error);
  $sql_query->bind_param('sssss', $_REQUEST['eit'], $_REQUEST['label'], $_REQUEST['loc'], $_REQUEST['label'], $_REQUEST['loc']) or die($conn->error);
  $sql_query->execute() or die($conn->error);
} else {
  // Attempt to fetch the item from SQL to populate the form
  $sql_query = $conn->prepare("SELECT label,loc FROM item_t WHERE eit = ?") or die($conn->error);
  $sql_query->bind_param('s', $_REQUEST['eit']) or die($conn->error);
  $sql_query->execute() or die($conn->error);
  $result = $sql_query->get_result();
  $item=$result->fetch_all(MYSQLI_ASSOC);

  // Copy for ease of use in HTML template
  $label = $item[0]['label'];
  $loc = $item[0]['loc'];
  $eit = $_REQUEST['eit'];
}

echo "<tr><td>Description</td><td><input name=\"label\" value=\"$label\"/></td></tr><tr><td>Location</td><td><input name=\"loc\" value=\"$loc\"></td></tr><input name=\"eit\" type=\"hidden\" value=\"$eit\"/>";
?>

<tr><td colspan=2><input type="submit" value="Save"/></td></tr>
</table>
</form>
</body>
</html>

