<?php

require_once './connection.php';

if (!isset($_POST['logout']) == "") {
    session_start();
    session_destroy();
    unset($_SESSION);
}

if (!isset($_POST['old_password']) == "" && !isset($_POST['new_password']) == "") {
//    echo 'old_password ' . $_POST['old_password'];
//    echo 'new_password ' . $_POST['new_password'];
    $old_password = $_POST['old_password'];
    $new_password = $_POST['new_password'];

    $sql = "SELECT * FROM tbl_login WHERE username='admin' and password=" . $old_password . "";

    if ($con) {
        $result = mysqli_query($con, $sql);
        $row = mysqli_fetch_assoc($result);
        $username = $row["username"];
//        echo 'username '.$username;
        if ($username=='admin') {
            $sql1 = "UPDATE tbl_login SET password=" . $new_password . " WHERE username='admin' and password=" . $old_password . "";

            if ($con->query($sql1) === TRUE) {
                echo "Record updated successfully";
            } else {
                echo "Error updating record: " . $con->error;
            }
        } else {
            echo "Old Password Not Match in Database !";
        }
    } else {
        echo 'Connection Problem';
    }

    $con->close();
}
?>

