Aim: Modify the above program such that it stores each query in a database and checks the database first for the result. If the query is already available in the DB, it returns the value that was previously computed (from DB) or it computes the result and returns it after storing the new query and result in DB

Program: PHP MySQL Calculator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP MySQL Calculator</title>
</head>
<body>
    <h2>Welcome</h2>
    <fieldset style="width:280px">
        <legend>Calculator</legend>
        <form method="post">
            <br/>Number 1: <input type="text" name="num1"><br/>
            <br/>Number 2: <input type="text" name="num2"><br/>
            <br/>Operation:   
            + <input type="radio" name="operation" value="+" checked>   
            - <input type="radio" name="operation" value="-">   
            * <input type="radio" name="operation" value="*">   
            / <input type="radio" name="operation" value="/">   
            <br/><br/>
            <input type="submit" name="submit" value="Calculate">
        </form>
    </fieldset>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $num1 = $_POST["num1"];
    $num2 = $_POST["num2"];
    $operation = $_POST["operation"];

    // Validate input
    if ($num1 === "" || $num2 === "") {
        echo "<p style='color:red;'>Please enter both numbers.</p>";
        exit;
    }
    if ($operation == "/" && $num2 == 0) {
        echo "<p style='color:red;'>Error: Division by zero is not allowed.</p>";
        exit;
    }

    // Database connection
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "calc";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Check if query exists
    $stmt = $conn->prepare("SELECT result FROM operations WHERE number1=? AND number2=? AND operation=?");
    $stmt->bind_param("dds", $num1, $num2, $operation);
    $stmt->execute();
    $stmt->bind_result($storedResult);
    $stmt->fetch();
    $stmt->close();

    echo '<fieldset style="width:280px"><legend>Result</legend>';
    
    if ($storedResult !== null) {
        echo "<p>Result (from DB): <strong>$storedResult</strong></p>";
    } else {
        // Perform calculation
        switch ($operation) {
            case "+": $result = $num1 + $num2; break;
            case "-": $result = $num1 - $num2; break;
            case "*": $result = $num1 * $num2; break;
            case "/": $result = $num1 / $num2; break;
        }

        echo "<p>Result (calculated): <strong>$result</strong></p>";

        // Store new calculation in DB
        $stmt = $conn->prepare("INSERT INTO operations (number1, number2, operation, result) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ddsd", $num1, $num2, $operation, $result);
        $stmt->execute();
        $stmt->close();
    }

    $conn->close();
    echo "</fieldset>";
}
?>
</body>
</html>
        
Output:

mysql calculator 1

Validations performed on above programmysql calculator 2mysql calculator 3

;