Created
August 17, 2020 05:10
-
-
Save mikelemus27/7f76f085403201d3844032edaaa14af7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($_GET['Enviar'])) { | |
//=============CONECTAR A DATA BASE============================================ | |
try { | |
$conn = new PDO('mysql:host=localhost;port=3307;dbname=operaciones', 'root', 'contrasena'); | |
}catch(PDOException $e){ | |
echo $e->getMessage(); | |
} | |
if ($conn==true){ | |
echo "Base de datos conectada<br>"; | |
}else{ | |
echo "Conexión fallida<br>"; | |
} | |
//Obtener operación de un Select box | |
$operacion = $_GET['operaciones']; | |
$num1 = $_GET["num1"]; | |
$num2 = $_GET['num2']; | |
switch ($operacion) { | |
case 'Sumar'; | |
try { | |
$sql = "CALL sumaphp($num1,$num2,@r);"; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
} catch (PDOException $e) { | |
echo $sql."<br>".$e->getMessage(); | |
} | |
break; | |
case 'Restar': | |
try { | |
$sql = "CALL restaphp($num1,$num2,@r);"; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
} catch (PDOException $e) { | |
echo $sql."<br>".$e->getMessage(); | |
} | |
break; | |
case 'Dividir': | |
try { | |
$sql = "CALL divisionphp($num1,$num2,@r);"; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
} catch (PDOException $e) { | |
echo $sql."<br>".$e->getMessage(); | |
} | |
break; | |
case 'Multiplicar': | |
try { | |
$sql = "CALL multiplicacionphp($num1,$num2,@r);"; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
} catch (PDOException $e) { | |
echo $sql."<br>".$e->getMessage(); | |
} | |
break; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Calculadora</title> | |
<link rel="stylesheet" href="css/bootstrap.css"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="icon" href="img/favicon-calc.png"> | |
</head> | |
<body> | |
<div class="container-fluid"> | |
<h2>Calculadora</h2> | |
<form action="calculadora.php" method="GET"> | |
<div class="form-group"> | |
<label>Número 1:</label> | |
<input class="form-control" type="number" name="num1" value="<?php echo $num1;?>" required="true"/> | |
</div> | |
<div class="form-group"> | |
<label>Número 2:</label> | |
<input class="form-control" type="number" name="num2" value="<?php echo $num2;?>" required="true"/> | |
</div> | |
<div class="form-group"> | |
<label>Operación: </label><select class="custom-select" id="operaciones" name="operaciones"> | |
<option value="Sumar">Sumar</option> | |
<option value="Restar">Restar</option> | |
<option value="Dividir">Dividir</option> | |
<option value="Multiplicar">Multiplicar</option> | |
</select> | |
</div> | |
<input class="btn btn-primary" type="submit" value="Enviar" name="Enviar"/> | |
</form> | |
<br /> | |
<div class="input-group mb-3"> | |
<div class="input-group-prepend"> | |
<span class="input-group-text">El resultado es: </span> | |
</div> | |
<?php | |
$sql = 'SELECT @r;'; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
if ($stmt->rowCount() > 0) { | |
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ | |
foreach($row as $value){ | |
$resultado = $value; | |
} | |
} | |
} else { | |
$resultado = "Consulta sin resultados"; | |
} | |
?> | |
<input class="form-control" type="number" disabled="true" value="<?php echo $resultado;?>"> | |
</div> | |
</div> | |
<center><p style="font-size: 11px">Copyright © 2020 Gabriel Z.R</p></center> | |
</body> | |
</html> | |
<?php | |
}else{ ?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Calculadora</title> | |
<link rel="stylesheet" href="css/bootstrap.css"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="icon" href="img/favicon-calc.png"> | |
</head> | |
<body> | |
<div class="container-fluid"> | |
<h2>Calculadora</h2> | |
<form action="calculadora.php" method="GET"> | |
<div class="form-group"> | |
<label>Número 1:</label> | |
<input class="form-control" type="number" name="num1" value="<?php echo $num1;?>" required="true"/> | |
</div> | |
<div class="form-group"> | |
<label>Número 2:</label> | |
<input class="form-control" type="number" name="num2" value="<?php echo $num2;?>" required="true"/> | |
</div> | |
<div class="form-group"> | |
<label>Operación: </label><select class="custom-select" id="operaciones" name="operaciones"> | |
<option value="Sumar">Sumar</option> | |
<option value="Restar">Restar</option> | |
<option value="Dividir">Dividir</option> | |
<option value="Multiplicar">Multiplicar</option> | |
</select> | |
</div> | |
<input class="btn btn-primary" type="submit" value="Enviar" name="Enviar"/> | |
</form> | |
<br /> | |
<div class="input-group mb-3"> | |
<div class="input-group-prepend"> | |
<span class="input-group-text">El resultado es: </span> | |
</div> | |
<input class="form-control" type="number" disabled="true" value=""> | |
</div> | |
</div> | |
<center><p style="font-size: 11px">Copyright © 2020 Gabriel Z.R</p></center> | |
</body> | |
</html> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment