Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- op.php
- <?php
- if($_POST["num1"]=="" || $_POST["num2"]=="")
- {
- if($_POST["num1"]=="")
- echo "Please enter Number 1 <br/>";
- if($_POST["num2"]=="")
- echo "Please enter Number 2 <br/>";
- }
- else
- if($_POST["num2"]==0)
- {
- echo "Number 2 should not be Zero. this is divided by zero error.";
- }
- else
- {
- echo $_POST["num1"] . " " . $_POST["Operation"] . " " . $_POST["num2"] . " = ";
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "calc";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT result FROM operations where number1=".$_POST["num1"]." and number2=".$_POST["num2"]." and operation='".$_POST["Operation"]."'" ;
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) {
- echo $row["result"];
- }
- echo "<br/> Result is displayed from database";
- }
- else {
- $resultVal=0;
- switch($_POST["Operation"])
- {
- case "-":
- echo $resultVal=$_POST["num1"] - $_POST["num2"];
- break;
- case "*":
- echo $resultVal=$_POST["num1"] * $_POST["num2"];
- break;
- case "/":
- echo $resultVal=$_POST["num1"] / $_POST["num2"];
- break;
- default:
- echo $resultVal=$_POST["num1"] + $_POST["num2"];
- break;
- }
- $sql = "INSERT INTO operations('number1','number2','operation','result')
- VALUES (".$_POST["num1"].",".$_POST["num2"].",'".$_POST["Operation"]."',".$resultVal.");";
- $result = $conn->query($sql);
- echo "<br/> This is first time operation is queried.";
- }
- $conn->close();
- }
- ?>
Add Comment
Please, Sign In to add comment