www/includes/db_inventory_hops.php

Wed, 29 Aug 2018 17:10:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 29 Aug 2018 17:10:31 +0200
changeset 45
95251bedfab4
parent 37
9362eb9e9e5b
child 77
a9f8de2d7b2b
permissions
-rw-r--r--

Moved three inventory tables and screens to profiles

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");
require($_SERVER['DOCUMENT_ROOT']."/version.php");


#Connect to the database
$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $connect) {
	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

// get data and store in a json array
$query = "SELECT * FROM inventory_hops ORDER BY origin,name";
if (isset($_GET['insert'])) {
	// INSERT COMMAND
	$sql  = "INSERT INTO `inventory_hops` SET name='" . mysqli_real_escape_string($connect, $_GET['name']);
	$sql .= "', alpha='" . $_GET['alpha'];
	$sql .= "', beta='" . $_GET['beta'];
	$sql .= "', humulene='" . $_GET['humulene'];
	$sql .= "', caryophyllene='" . $_GET['caryophyllene'];
	$sql .= "', cohumulone='" . $_GET['cohumulone'];
	$sql .= "', myrcene='" . $_GET['myrcene'];
	$sql .= "', hsi='" . $_GET['hsi'];
	$sql .= "', type='" . $_GET['type'];
	$sql .= "', form='" . $_GET['form'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']);
	$sql .= "', origin='" . mysqli_real_escape_string($connect, $_GET['origin']);
	$sql .= "', substitutes='" . mysqli_real_escape_string($connect, $_GET['substitutes']);
	($_GET['always_on_stock'] == 'true') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
	$sql .= "', inventory='" . floatval($_GET['inventory']) / 1000.0;
	$sql .= "', cost='" . $_GET['cost'];
	$sql .= "', production_date='" . $_GET['production_date'];
	$sql .= "', tht_date='" . $_GET['tht_date'];
	$sql .= "', total_oil='" . $_GET['total_oil'];
	$sql .= "';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_hops: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_hops: inserted ".$_GET['name']);
	}
	echo $result;

} else if (isset($_GET['update'])) {
	// UPDATE COMMAND
	$sql  = "UPDATE `inventory_hops` SET name='" . mysqli_real_escape_string($connect, $_GET['name']);
	$sql .= "', alpha='" . $_GET['alpha'];
	$sql .= "', beta='" . $_GET['beta'];
	$sql .= "', humulene='" . $_GET['humulene'];
	$sql .= "', caryophyllene='" . $_GET['caryophyllene'];
	$sql .= "', cohumulone='" . $_GET['cohumulone'];
	$sql .= "', myrcene='" . $_GET['myrcene'];
	$sql .= "', hsi='" . $_GET['hsi'];
	$sql .= "', type='" . $_GET['type'];
	$sql .= "', form='" . $_GET['form'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']);
	$sql .= "', origin='" . mysqli_real_escape_string($connect, $_GET['origin']);
	$sql .= "', substitutes='" . mysqli_real_escape_string($connect, $_GET['substitutes']);
	($_GET['always_on_stock'] == 'true') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
	$sql .= "', inventory='" . floatval($_GET['inventory']) / 1000.0;
	$sql .= "', cost='" . $_GET['cost'];
	$sql .= "', production_date='" . $_GET['production_date'];
	$sql .= "', tht_date='" . $_GET['tht_date'];
	$sql .= "', total_oil='" . $_GET['total_oil'];
	$sql .= "' WHERE record='" . $_GET['record'] . "';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_hops: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_hops: updated record ".$_GET['record']);
	}
	echo $result;

} else if (isset($_GET['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `inventory_hops` WHERE record='".$_GET['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_hops: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_hops: deleted record ".$_GET['record']);
	}
	echo $result;

} else {
	// SELECT COMMAND
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$hops[] = array(
			'record' => $row['record'],
			'name' => $row['name'],
			'alpha' => $row['alpha'],
			'beta' => $row['beta'],
			'humulene' => $row['humulene'],
			'caryophyllene' => $row['caryophyllene'],
			'cohumulone' => $row['cohumulone'],
			'myrcene' => $row['myrcene'],
			'hsi' => $row['hsi'],
			'type' => $row['type'],
			'form' => $row['form'],
			'notes' => $row['notes'],
			'origin' => $row['origin'],
			'substitutes' => $row['substitutes'],
			'always_on_stock' => $row['always_on_stock'],
			'inventory' => floatval($row['inventory']) * 1000.0,
			'cost' => $row['cost'],
			'production_date' => $row['production_date'],
			'tht_date' => $row['tht_date'],
			'total_oil' => $row['total_oil']
		);
	}
	echo json_encode($hops);
}
?>

mercurial