Guest User

Untitled

a guest
Mar 29th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. op.php
  2. <?php
  3.  
  4.    
  5.  
  6.  
  7.     if($_POST["num1"]=="" || $_POST["num2"]=="")
  8.     {
  9.         if($_POST["num1"]=="")
  10.             echo "Please enter Number 1 <br/>";
  11.         if($_POST["num2"]=="")
  12.             echo "Please enter Number 2 <br/>";
  13.     }
  14.     else
  15.         if($_POST["num2"]==0)
  16.     {
  17.        
  18.             echo "Number 2 should not be Zero. this is divided by zero error.";
  19.     }
  20.     else
  21.     {
  22.        
  23.     echo $_POST["num1"] . " " . $_POST["Operation"] . " " . $_POST["num2"] . " = ";
  24.    
  25.     $servername = "localhost";
  26.     $username = "root";
  27.     $password = "";
  28.     $dbname = "calc";
  29.  
  30.     // Create connection
  31.     $conn = new mysqli($servername, $username, $password, $dbname);
  32.     // Check connection
  33.     if ($conn->connect_error) {
  34.         die("Connection failed: " . $conn->connect_error);
  35.     }
  36.  
  37.     $sql = "SELECT result FROM operations where number1=".$_POST["num1"]." and number2=".$_POST["num2"]." and operation='".$_POST["Operation"]."'" ;
  38.     $result = $conn->query($sql);
  39.  
  40.     if ($result->num_rows > 0) {
  41.         // output data of each row
  42.         while($row = $result->fetch_assoc()) {
  43.             echo $row["result"];
  44.         }
  45.     echo "<br/> Result is displayed from database";
  46.     }
  47.     else {
  48.         $resultVal=0;
  49.    
  50.         switch($_POST["Operation"])
  51.         {
  52.             case "-":
  53.                echo $resultVal=$_POST["num1"] - $_POST["num2"];            
  54.                 break;             
  55.             case "*":
  56.                echo $resultVal=$_POST["num1"] * $_POST["num2"];
  57.                break;              
  58.             case "/":
  59.                echo $resultVal=$_POST["num1"] / $_POST["num2"];
  60.                break;              
  61.             default:
  62.                echo $resultVal=$_POST["num1"] + $_POST["num2"];            
  63.                break;              
  64.         }
  65.        
  66.         $sql = "INSERT INTO operations('number1','number2','operation','result')
  67.                 VALUES (".$_POST["num1"].",".$_POST["num2"].",'".$_POST["Operation"]."',".$resultVal.");";
  68.         $result = $conn->query($sql);
  69.         echo "<br/> This is first time operation is queried.";
  70.     }
  71.     $conn->close();
  72.  
  73.        
  74.        
  75.     }      
  76.  
  77.  
  78.  
  79. ?>
Add Comment
Please, Sign In to add comment