miércoles, 11 de diciembre de 2019

MySQL / PHP Inserting Data into a MySQL Database Table - Average Column Value with specific Parameters

index.php

<html>
<style>
body { font-family: helvetica;}
</style>
<body>
<center>
<h1>Calificaciones</h1>
<a href='nuevo.php'>Nueva entrada</a><br><br>
<?php
error_reporting(0);
include 'conexion.php';

echo '<table border="1" cellspacing="2" cellpadding="2">
      <tr>
          <th> <font face="Arial">ID</font> </th>
          <th> <font face="Arial">Nombre</font> </th>
          <th> <font face="Arial">C1</font> </th>
          <th> <font face="Arial">C2</font> </th>
          <th> <font face="Arial">C3</font> </th>
          <th> <font face="Arial">Promedio</font> </th>
          <th> <font face="Arial">Aprobado</font> </th>
          <th> <font face="Arial">Borrar</font> </th>
      </tr>';

if ($result = $mysqli->query($query)) {


    while ($row = $result->fetch_assoc()) {
        $field1name = $row["id"];
        $field2name = $row["nombre"];
        $field3name = $row["c1"];
        $field4name = $row["c2"];
        $field5name = $row["c3"];
        $avg = ($field3name+$field4name+$field5name)/3;



        echo '<tr>
                  <td>'.$field1name.'</td>
                  <td>'.$field2name.'</td>
                  <td>'.$field3name.'</td>
                  <td>'.$field4name.'</td>
                  <td>'.$field5name.'</td>';

                          echo '<td>'.$avg.'</td>';

                if($avg>=6){
        echo '<td>APROBADO</td>';
        }else{echo '<td>NO APROBADO</td>';}

        echo "<td><a href='borrar.php?id=".$field1name."'>Borrar</a></td>";




        echo '<tr>';

    }
    $result->free();
}


$name = $_GET["nombre"];
$cal1 = $_GET["cal1"];
$cal2 = $_GET["cal2"];
$cal3 = $_GET["cal3"];
?>


<center>
</body>
</html>





nuevo.php

<html>
<style>
body { font-family: helvetica;}
</style>
<head>
<meta charset="UTF-8">
<title>Nueva entrada</title>
</head>
<body>
<center>
<h1>Nueva entrada</h1>
<form action="insertar.php" method="post">
<b>
    <p>
        <label for="nombre">Nombre:</label>
        <input type="text" name="nombre" id="nombre">
    </p>
    <p>
        <label for="cal1">Calificación 1</label>
        <input type="number" name="cal1" id="cal1">
    </p>
    <p>
        <label for="cal2">Calificación 2</label>
        <input type="number" name="cal2" id="cal2">
    </p>
    <p>
        <label for="cal3">Calificación 3</label>
        <input type="number" name="cal3" id="cal3">
    </p>
    <input type="submit" value="Agregar">
</form>
<br><br>
<a href='index.php'>Volver</a>
</center>
</body>

</html>




insertar.php

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "ordi");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$nombre = mysqli_real_escape_string($link, $_REQUEST['nombre']);
$cal1 = mysqli_real_escape_string($link, $_REQUEST['cal1']);
$cal2 = mysqli_real_escape_string($link, $_REQUEST['cal2']);
$cal3 = mysqli_real_escape_string($link, $_REQUEST['cal3']);

$sql = "INSERT INTO alumnos (id, nombre, c1, c2, c3) VALUES ('', '$nombre', '$cal1', '$cal2', '$cal3')";
if(mysqli_query($link, $sql)){
    echo "La entrada se añadió correctamete.";
    header("Location: index.php");
} else{
    echo "ERROR: Entrada incorrecta: $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
exit();

?>





borrar.php

<?php
error_reporting(0);
$id = $_GET['id'];
include 'conexion.php';

$sql = "DELETE FROM alumnos WHERE id = $id";

if (mysqli_query($conn, $sql)) {
    mysqli_close($conn);
    header('Location: index.php');
    exit;
} else {
    echo "Error al borrar";

}



conexion.php

<?php
$username = "root";
$password = "";
$database = "ordi";
$dbname = "ordi";
$servername = "localhost";

$mysqli = new mysqli("localhost", $username, $password, $database);
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM alumnos";

No hay comentarios:

Publicar un comentario