<?php
include ("../../include/session.php");
include ("../../include/db.php");
if (!$session->isAdmin()) {
	header("Location: ../../main.php");
} else {
	$recipieId = mysql_real_escape_string($_POST['recipieId']);
	$recName = mysql_real_escape_string($_POST['recipieName']);
	foreach ($_POST as $key => $value) {
		$fieldArray[$key] = mysql_real_escape_string($value);
		$fields = mysql_list_fields('Cody', 'recipiedepalet');
		$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" && $key != "recipieName" && $key != "size") {
				if (preg_match("/Price/i", $key)) {
					$bleh = str_replace("Price", "", $key);
					$query1 = "ALTER TABLE nam_perdepaletprice ADD $bleh double(10,2)";
					execsql($query1);
				} else {

					$query = "ALTER TABLE nam_recipiedepalet ADD $key double(10,4)";
					$result = execsql($query);

				}
			}
		}
	}

	$columns = "";
	$values = "";
	$q = "";
	foreach ($fieldArray as $key => $value) {
		if ($key != "submitok" && $value != "") {
			if (preg_match("/Price/i", $key)) {
				$bleh = str_replace("Price", "", $key);
				$pColumns .= "$bleh,";
				$pValues .= "'$value',";
				$pUpdate .= "$bleh = $value,";
			} else {
				$columns .= "$key,";
				$values .= "'$value',";
				if($key != "recipieName"){
					$update .= "$key = $value,";
				}
			}
		}
	}
	if($recipieId == ""){
		$q = "INSERT INTO nam_recipiedepalet ($columns id) VALUES ($values 'null')";
		$q1 = "INSERT INTO nam_perdepaletprice ($pColumns id, recipieName) VALUES ($pValues 'null', '$recName')";
	}else{
		$q = "UPDATE nam_recipiedepalet SET $update recipieName = '$recName' WHERE id = $recipieId";
		$q1 = "UPDATE nam_perdepaletprice SET $pUpdate recipieName = '$recName' WHERE id = $recipieId";

	}
//	echo $update ."<br />";
//	echo $q ."<br /><br />". $q1;
		$r = execsql($q);
		execsql($q1);
	if (!$r) {
		echo "you fail";
	} else {
		header("Location: ../productionMenu.php");
	}

}
?>
