<?php
// forget_password.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];

    // $conn = new mysqli('localhost', 'root', '', 'teacher_portal');
    include '../config.php';

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

    $stmt = $conn->prepare("SELECT id FROM teachers WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $reset_token = bin2hex(random_bytes(16));
        $stmt->close();

        $stmt = $conn->prepare("UPDATE teachers SET reset_token = ? WHERE email = ?");
        $stmt->bind_param("ss", $reset_token, $email);
        $stmt->execute();

        // Here you'd send the reset token via email with a reset link
       // echo "Password reset link has been sent to your email.";
         header("Location: ../forget_password.php");
       $_SESSION['mess'] = 'Password reset link has been sent to your email.';
    } else {
         $_SESSION['errmes'] = "No account found with that email.";
    }

    $stmt->close();
    $conn->close();
}
?>