<?php require_once('Connections/food.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE typefood SET t_name=%s WHERE t_id=%s",
                       GetSQLValueString($_POST['t_name'], "text"),
                       GetSQLValueString($_POST['t_id'], "int"));

  mysql_select_db($database_food, $food);
  $Result1 = mysql_query($updateSQL, $food) or die(mysql_error());

  $updateGoTo = "add_typefood.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_food, $food);
$t_id = $_GET['t_id'];
$query_type = "SELECT * FROM typefood where t_id = " .$t_id;
$type = mysql_query($query_type, $food) or die(mysql_error());
$row_type = mysql_fetch_assoc($type);
$totalRows_type = mysql_num_rows($type);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>แก้ไขประเภทอาหาร</title>
<style type="text/css">
body,td,th {
	font-family: "Angsana New";
	font-size: 16pt;
}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
}
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center" valign="middle"><img src="pic/banner.png" width="1000" height="274" /></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a href="index.php">หน้าแรก</a>        </li>
      <li><a href="#" class="MenuBarItemSubmenu">บันทึกข้อมูล</a>
        <ul>
          <li><a href="add_typefood.php">บันทึกประเภทอาหาร</a></li>
          <li><a href="add_food.php">บันทึกอาหาร</a></li>
        </ul>
      </li>
      <li><a class="MenuBarItemSubmenu" href="#">ค้นหาข้อมูล</a>
        <ul>
<li><a href="search_typefood.php">ค้นหาประเภทอาหาร</a></li>
          <li><a href="search_food.php">ค้นหาอาหาร</a></li>
        </ul>
      </li>
</ul></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><br />
      <strong>แก้ไขประเภทอาหาร</strong>
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
        <table width="500" align="center">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">รหัสประเภทอาหาร :</td>
            <td><?php echo $row_type['t_id']; ?></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">ประเภทอาหาร:</td>
            <td><input type="text" name="t_name" value="<?php echo htmlentities($row_type['t_name'], ENT_COMPAT, 'utf-8'); ?>" size="50" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="แก้ไขข้อมูล" /></td>
          </tr>
        </table>
        <input type="hidden" name="MM_update" value="form1" />
        <input type="hidden" name="t_id" value="<?php echo $row_type['t_id']; ?>" />
      </form>
    <p>&nbsp;</p></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><img src="pic/footer.png" width="1000" height="135" /></td>
  </tr>
</table>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>
<?php
mysql_free_result($type);
?>
