
<!DOCTYPE html>
<html lang="en">

 <?php include 'head.php';?>

<body>

  <!-- ======= Header ======= -->
  
 <?php include 'menubar.php';?>


 <?php include 'sidebar.php';?>


  <main id="main" class="main">

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "tslibrary_user", "rb!tsl838", "tslibrary_data");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 $studentid =$_POST["student_id"]; 
// Attempt select query execution
$sql = "SELECT  * FROM student_registration where student_id='$studentid'";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        
        while($row = mysqli_fetch_array($result)){
          
              $studentid=$row['student_id'];
			  echo "Dublicate Student ID";
			  exit;
          
        }
    
        // Free result set
        mysqli_free_result($result);
    } else{
        
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 $studentid= $studentid+1;
// Close connection
mysqli_close($link);
?>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Database connection details
$servername = "localhost";
$username = "tslibrary_user";
$password = "rb!tsl838";
 $database = "tslibrary_data";

    // Create a database connection
    $conn = new mysqli($servername, $username, $password, $database);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $name = $conn->real_escape_string($_POST["name"]);
    $father = $conn->real_escape_string($_POST["father"]);
    $mother = $conn->real_escape_string($_POST["mother"]);
    $dob = $conn->real_escape_string($_POST["dob"]);
    $address = $conn->real_escape_string($_POST["address"]);
    $mobile = $conn->real_escape_string($_POST["mobile"]);
    $mobile2 = $conn->real_escape_string($_POST["mobile2"]);
    $aadhar = $conn->real_escape_string($_POST["aadhar"]);
    $reg_date = $conn->real_escape_string($_POST["day"]);
    $reg_month = $conn->real_escape_string($_POST["reg_month"]);
    $reg_year = $conn->real_escape_string($_POST["reg_year"]);
    $fees_amount = $conn->real_escape_string($_POST["fees_amount"]);
	 $fees_deposit_date = $_POST["day"].'/'.$_POST["month"].'/'.$_POST["year"];
    $fees_deposit_date = $conn->real_escape_string($fees_deposit_date);
    $fees_mode = $conn->real_escape_string($_POST["fees_mode"]);
    $student_id = $conn->real_escape_string($_POST["student_id"]);
    $batch_time = $conn->real_escape_string($_POST["batch_time"]);
	$username = $conn->real_escape_string($_POST["username"]);
	$password = $conn->real_escape_string($_POST["password"]);
	$referid = $conn->real_escape_string($_POST["referid"]);

    // Handle file upload for the photo
    $photo = $_FILES["photo"];
    $photo_name = $conn->real_escape_string($photo["name"]);
    $photo_tmp = $photo["tmp_name"];

    // Resize the photo to 300x300 pixels
    list($width, $height) = getimagesize($photo_tmp);
    $new_width = 300;
    $new_height = 300;
    $resized_image = imagecreatetruecolor($new_width, $new_height);
    $source_image = imagecreatefromstring(file_get_contents($photo_tmp));
    imagecopyresampled($resized_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    // Save the resized photo to a folder on your server
    $photo_destination = "uploads/".$photo_name;
    imagejpeg($resized_image, $photo_destination, 100);
    imagedestroy($resized_image);
	$photo_name = $photo_name;
    // Insert data into the database
    $sql = "INSERT INTO student_registration (name, father, mother, dob, address, mobile, mobile2, aadhar, reg_date, reg_month, reg_year, fees_amount, fees_deposit_date, fees_mode, student_id, batch_time, photo,username,password,branch_name,referid)
    VALUES ('$name', '$father', '$mother', '$dob', '$address', '$mobile', '$mobile2', '$aadhar', '$reg_date', '$reg_month',  
	'$reg_year', '$fees_amount', '$fees_deposit_date', '$fees_mode', '$student_id', '$batch_time', '$photo_name','$username', '$password','$user','$referid')";

    if ($conn->query($sql) === TRUE) {
        echo "Registration successful.";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    // Close the database connection
    $conn->close();
} else {
    echo "Invalid request.";
}
?>

<?php
// Assuming you have a database connection established
$servername = "localhost";
$username = "tslibrary_user";
$password = "rb!tsl838";
$dbname = "tslibrary_data";
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
	$fees_dep_date= $_POST["day"];
	$fees_dep_month= $_POST["month"];
	$fees_dep_year= $_POST["year"];
// SQL query to insert data into the table
$sql = "INSERT INTO monthly_fees (student_id, fees_month, fees_year, paid_fees, payment_date,student_name,student_mobile1,student_mobile2,total_present_days,deposit_fees_date,deposit_fees_month,deposit_fees_year) 
        VALUES ('$student_id', '$reg_month', '$reg_year', '$fees_amount', '$fees_deposit_date','$name', 
		'$mobile', '$mobile2','0','$fees_dep_date','$fees_dep_month','$fees_dep_year')";

if ($conn->query($sql) === TRUE) {
    echo "Fees Record successfully Updated";
} else {
    echo "Error inserting record: " . $conn->error;
}

// Close the database connection
$conn->close();
?>

<h1><center>Successfully register Student </center></h1><br>
<a href="viewregistraion.php">Go Back and Download Admission Slip</a>
  </main><!-- End #main -->

   <?php include 'footer.php';?>

</body>

</html>