<?php require_once('Connections/conn.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("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_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 students SET surname=%s, name=%s,middle=%s, address=%s, `class`=%s, whereabout = %s, admission_no = %s WHERE s_no=%s",
                       GetSQLValueString($_POST['surname'], "text"),
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['middle'], "text"),
					   GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['class'], "text"),
					   GetSQLValueString($_POST['whereabout'], "text"),
                       GetSQLValueString($_POST['admission_no'], "int"),
					   GetSQLValueString($_POST['s_no'], "int"));

  mysqli_select_db($conn, $database_conn);
  $Result1 = mysqli_query($conn, $updateSQL) or die(mysqli_error($conn));

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

$colname_Recordset1 = "-1";
if (isset($_GET['admission_no'])) {
  $colname_Recordset1 = $_GET['admission_no'];
}
mysqli_select_db($conn, $database_conn);
$query_Recordset1 = sprintf("SELECT * FROM students WHERE admission_no = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysqli_query($conn, $query_Recordset1) or die(mysqli_error($conn));
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysqli_num_rows($Recordset1);

mysqli_free_result($Recordset1);
?>


<html>
<head>
<title> Update history</title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<body>
<?php include("links.php");?>
<div class="container" >

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
  <tr>
  <td colspan="2"><h4> Update Student details </h4></td>
  </tr>
   <tr>
   <td height="33" align="right" nowrap><div align="right">Surname:</div></td>
   <td><div align="left"><input type="textfield" name="surname" value="<?php echo $row_Recordset1['surname']; ?>">
   </div></td></tr>
    <tr>
   <td height="37" align="right" nowrap><div align="right">Firstname:</div></td>
   <td><div align="left"><input type="textfield" name="name" value="<?php echo $row_Recordset1['name']; ?>">
   </div></td>
   </tr>
    <tr>
   <td height="38" align="right" nowrap><div align="right">Middle Name:</div></td>
   <td><div align="left"><input type="textfield" name="middle" value="<?php echo $row_Recordset1['middle']; ?>">
   </div></td>
   </tr>
    <tr >
      <td height="29" align="right" nowrap><div align="right">Admission_no:</div></td>
      <td><div align="left"><input type="textfield" name="admission_no" value="<?php echo $row_Recordset1['admission_no']; ?>">
	</div></td>
    </tr>
     <tr >
      <td height="29" align="right" nowrap><div align="right">Whereabout:</div></td>
      <td><div align="left"><select name="whereabout">
							  <!--<option selected="selected" value="">Select</option>-->
							  <option value="School" <?php if($row_Recordset1['whereabout'] == "School") { ?> selected="selected" <?php } ?> >School</option>
							  <option value="Vacation" <?php if($row_Recordset1['whereabout'] == "Vacation") { ?> selected="selected" <?php } ?>>Vacation</option>
							   <option value="Absent" <?php if($row_Recordset1['whereabout'] == "Absent") { ?> selected="selected" <?php } ?>>Absent</option>
                              </select></div></td>
    </tr>
    <tr>
      <td height="110" align="right" valign="top" nowrap><div align="right"> Additional Info:</div></td>
      <td><div align="left"><textarea name="address" cols="50" rows="5"><?php echo htmlentities($row_Recordset1['address'], ENT_COMPAT, ''); ?></textarea></div></td>
    </tr>
    <tr >
      <td height="39" align="right" nowrap><div align="right">Class:</div></td>
      <td><div align="left"><select name="class">
							  <option selected="selected" value="">Select</option>
							  <option value="JS1" <?php if($row_Recordset1['class'] == "JS1") { ?> selected="selected" <?php } ?> >JS1</option>
							  <option value="JS2" <?php if($row_Recordset1['class'] == "JS2") { ?> selected="selected" <?php } ?>>JS2</option>
							   <option value="JS3" <?php if($row_Recordset1['class'] == "JS3") { ?> selected="selected" <?php } ?>>JS3</option>
                                <option value="SS1" <?php if($row_Recordset1['class'] == "SS1") { ?> selected="selected" <?php } ?>>SS1</option>
							  <option value="SS2" <?php if($row_Recordset1['class'] == "SS2") { ?> selected="selected" <?php } ?>>SS2</option>
							   <option value="SS3" <?php if($row_Recordset1['class'] == "SS3") { ?> selected="selected" <?php } ?>>SS3</option>
							 
							  
  </select></div></td>
    </tr>
    <tr >
      <td height="55" align="right" nowrap>&nbsp;</td>
      <td><div align="left"><input type="submit" value="Update record">  <a href="students.php" ><strong>CANCEL UPDATE</strong> </a></div></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1">
<?php if (isset($_GET['s_no'])) {  ?>
 <input type="hidden" name="s_no" value="<?php echo $_GET['s_no']; ?>"> 
<?php } ?>  

</form>

</div>

<br />
 </body>
 
 </html>

