<?php
session_start();
include('connect.php');
if(!empty($_POST["fname"])) {
$sql = "INSERT INTO student (tname, fname, lname, cid, bdate, phone, studentsp, spdetail, oldschool, lev, projects, ref2, samugtype, samugstatus) VALUES ";

$sql .= "('".$_POST["tname"]."', '".$_POST["fname"]."', '".$_POST["lname"]."', '".$_POST["cid"]."', '".$_POST["bdate"]."', '".$_POST["phone"]."', '".$_POST["studentsp"]."', '".$_POST["spdetail"]."', '".$_POST["oldschool"]."', '1', '1', '".sprintf("%06d","1")."' ,'online', 'regis');";

if ($conn->query($sql) === TRUE) {
	$last_id = $conn->insert_id;
	
	$sqlu = "UPDATE student SET ref1='".sprintf("%010d",$last_id)."' WHERE id = '".$last_id."'";
	$conn->query($sqlu) ;
$target_dir = "studentpic/";
mkdir($target_dir.sprintf("%010d",$last_id));
$upload_extension = explode(".",basename($_FILES['fileToUpload']['name']));		
$target_file = $target_dir.sprintf("%010d",$last_id)."/P".sprintf("%010d",$last_id).'.'.end($upload_extension);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);	
	
$upload_extension2 = explode(".",basename($_FILES['fileToUpload4']['name']));		
$target_file2 = $target_dir.sprintf("%010d",$last_id)."/T".sprintf("%010d",$last_id).'.'.end($upload_extension2);
move_uploaded_file($_FILES["fileToUpload4"]["tmp_name"], $target_file2);		
	
$sqlu = "UPDATE student SET ref1='".sprintf("%010d",$last_id)."', studentpic='".$target_file."', transcript='".$target_file2."'  WHERE id = '".$last_id."'";
$conn->query($sqlu) ;
$_SESSION["ref1"] = sprintf("%010d",$last_id);	
echo sprintf("%010d",$last_id);	
} else {
  echo "ErrorDB";
}
}else{
	echo "ErrorFrom";
	header('location: https://www.ws.ac.th/quotam1/');
}
?>
