<?php
include ("../../include/session.php");
include ("../../include/db.php");
if (!$session->isAdmin()) {
	header("Location: ../../main.php");
} else {
	$recipieName = mysql_real_escape_string($_POST['recipieName']);
	foreach ($_POST as $key => $value) {
		$fieldArray[$key] = mysql_real_escape_string($value);
		$fields = mysql_list_fields('Cody', 'perproductprice');
		$columns = mysql_num_fields($fields);
		for ($i = 0; $i < $columns; $i++) {
			$field_array[] = mysql_field_name($fields, $i);
		}
		if (!in_array($key, $field_array)) {
			if ($key != "submitok") {
				if (preg_match("/Price/i", $key)) {
					$bleh = str_replace("Price", "", $key);
					$query1 = "ALTER TABLE perproductprice ADD $bleh double(10,2)";
//					echo $query1;
					execsql($query1);
				} else {
					$query = "ALTER TABLE recipie ADD $key double(10,4)";
					$result = execsql($query);
				}
			}
		}

	}

	$columns = "";
	$values = "";
	$pColumns = "";
	$pValues = "";
	$q = "";
	foreach ($fieldArray as $key => $value) {
		if ($key != "submitok" && $key != "recipieName" && $value != "") {
			if (preg_match("/Price/i", $key)) {
				$bleh = str_replace("Price", "", $key);
				$pColumns .= "$bleh,";
				$pValues .= "'$value',";
			} else {
				$columns .= "$key,";
				$values .= "'$value',";
			}
		}
	}
	$q = "INSERT INTO recipie ($columns recipieName) VALUES ($values '" . $recipieName . "')";
	$q1 = "INSERT INTO perproductprice($pColumns recipieName) VALUES ($pValues '" . $recipieName . "')";
//			echo $q;
	$r = execsql($q);
	$r1 = execsql($q1);
	if (!$r) {
		echo "you fail";
	} else {
		$return = mysql_real_escape_string($_POST['submitok']);
if ($return == "Save and New") {
				$cat = mysql_real_escape_string($_POST['cat']);
				header("Location: ../addRecipie.php");
			} else {
				header("Location: ../productionMenu.php");
			}
	}

}
?>
