<?php
include('connection.php');
session_start();

if (isset($_POST['Submit'])) {
    // Get user input
    $AccID = $_POST['AccID'];
    $AccNPass = $_POST['AccNPass'];
    
    // Prepare SQL statement to check if the ID exists
    $sql = "SELECT Renter_ID FROM renter WHERE Renter_ID = ?";
    
    // Prepare and execute the statement with prepared statements
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $AccID);
    $stmt->execute();
    
    // Get the result set
    $result = $stmt->get_result();
    
    // Check if there's a matching user
    if ($result->num_rows > 0) {
        // Update password if user exists
        $update_sql = "UPDATE renter SET Renter_Pass = ? WHERE Renter_ID = ?";
        $update_stmt = $conn->prepare($update_sql);
        $update_stmt->bind_param("ss", $AccNPass, $AccID);
        
        if ($update_stmt->execute()) {
            // Password updated successfully
            echo "<script>alert('Reset Password successfully')</script>";
            echo "<script>window.location = 'login.html'</script>";
            exit();
        }
    } 
    else {
        echo "<script>alert('Wrong ID or Password')</script>";
        echo "<script>window.location = 'forgot.html'</script>";
        exit();
    }
}
?>
