www/includes/db_inventory_water.php

Fri, 28 Sep 2018 17:29:23 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 28 Sep 2018 17:29:23 +0200
changeset 72
93a0be4f5be3
parent 47
94cd5ac04b6a
child 77
a9f8de2d7b2b
permissions
-rw-r--r--

Added category and type to the recipe style. Added tooltips in the edit screen. Added Alcohol and Carbonation to the recipe style. Redesigned the main edit window and added the style limits.

<?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());
}

/*
 * If a new default will be set, clear the old default.
 */
function clear_default() {
	global	$connect;

	$usql = "UPDATE inventory_waters SET default_water='0' WHERE (default_water = '1');";
	$result = mysqli_query($connect, $usql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_waters: " . $usql . "  result: ".mysqli_error($connect));
	}
}


// get data and store in a json array
$query = "SELECT * FROM inventory_waters ORDER BY name";
if (isset($_GET['insert'])) {
	// INSERT COMMAND
	$sql  = "INSERT INTO `inventory_waters` SET name='" . mysqli_real_escape_string($connect, $_GET['name']);
	($_GET['unlimited_stock'] == 'true') ? $sql .= "', unlimited_stock='1" : $sql .= "', unlimited_stock='0";
	$sql .= "', calcium='" . $_GET['calcium'];
	$sql .= "', bicarbonate='" . $_GET['bicarbonate'];
	$sql .= "', sulfate='" . $_GET['sulfate'];
	$sql .= "', chloride='" . $_GET['chloride'];
	$sql .= "', sodium='" . $_GET['sodium'];
	$sql .= "', magnesium='" . $_GET['magnesium'];
	$sql .= "', ph='" . $_GET['ph'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']);
	$sql .= "', total_alkalinity='" . $_GET['total_alkalinity'];
	if ($_GET['default_water'] == 'true') {
		clear_default();
		$sql .= "', default_water='1";
	} else {
		$sql .= "', default_water='0";
	}
	$sql .= "', inventory='" . $_GET['inventory'];
	$sql .= "', cost='" . $_GET['cost'];
	$sql .= "';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_waters: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_waters: inserted ".$_GET['name']);
	}
	echo $result;

} else if (isset($_GET['update'])) {
	// UPDATE COMMAND
	$sql  = "UPDATE `inventory_waters` SET name='" . mysqli_real_escape_string($connect, $_GET['name']);
	($_GET['unlimited_stock'] == 'true') ? $sql .= "', unlimited_stock='1" : $sql .= "', unlimited_stock='0";
	$sql .= "', calcium='" . $_GET['calcium'];
	$sql .= "', bicarbonate='" . $_GET['bicarbonate'];
	$sql .= "', sulfate='" . $_GET['sulfate'];
	$sql .= "', chloride='" . $_GET['chloride'];
	$sql .= "', sodium='" . $_GET['sodium'];
	$sql .= "', magnesium='" . $_GET['magnesium'];
	$sql .= "', ph='" . $_GET['ph'];
	$sql .= "', notes='" . mysqli_real_escape_string($connect, $_GET['notes']);
	$sql .= "', total_alkalinity='" . $_GET['total_alkalinity'];
	if ($_GET['default_water'] == 'true') {
		clear_default();
		$sql .= "', default_water='1";
	} else {
		$sql .= "', default_water='0";
	}
	$sql .= "', inventory='" . $_GET['inventory'];
	$sql .= "', cost='" . $_GET['cost'];
	$sql .= "' WHERE record='" . $_GET['record'] . "';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_waters: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_waters: updated record ".$_GET['record']);
	}
	echo $result;

} else if (isset($_GET['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `inventory_waters` WHERE record='".$_GET['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_inventory_waters: ".$sql." result: ".mysqli_error($connect));
	} else {
		syslog(LOG_NOTICE, "db_inventory_waters: 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)) {
		$waters[] = array(
			'record' => $row['record'],
			'name' => $row['name'],
			'unlimited_stock' => $row['unlimited_stock'],
			'calcium' => $row['calcium'],
			'bicarbonate' => $row['bicarbonate'],
			'sulfate' => $row['sulfate'],
			'chloride' => $row['chloride'],
			'sodium' => $row['sodium'],
			'magnesium' => $row['magnesium'],
			'ph' => $row['ph'],
			'notes' => $row['notes'],
			'total_alkalinity' => $row['total_alkalinity'],
			'default_water' => $row['default_water'],
			'inventory' => $row['inventory'],
			'cost' => $row['cost']
		);
	}
	echo json_encode($waters);
}
?>

mercurial