Sol crud

De wikiserver
La revisió el 00:13, 31 gen 2018 per Jnoguera (Discussió | contribucions)
(dif) ← Versió més antiga | Versió actual (dif) | Versió més nova → (dif)
Dreceres ràpides: navegació, cerca

INDEX.PHP

<?php
require("conexion.php");

$sql1 = "select * from tcrud";

if (!$resul = mysqli_query($conn, $sql1)) {
    echo "No s'ha pogut realitzar la consulta";
    echo mysqli_error($conn);
    exit;
}

?>
<html>

<div class="container">
    <div class="row">
        <a href="index.php">HOME</a>   |   <a href="create.php">CREAR</a>
    </div>
    <div class="row">
        <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
            <thead>
            <tr>
                <th>Nombre</th>
                <th>Fecha</th>
                <th>Datos</th>
                <th>Opciones</th>
            </tr>
            </thead>
            <tbody>
            <br>
            <?php

            while ($arr_resul = mysqli_fetch_array($resul)) {
                echo "<tr>";
                echo "<td>" . $arr_resul['nombre'] . "</td>";
                echo "<td>" . $arr_resul['fecha'] . "</td>";
                echo "<td>" . $arr_resul['dato'] . "</td>";
                echo "<td><a href=\"edit.php?id=$arr_resul[id]\">Edit</a> | <a href=\"delete.php?id=$arr_resul[id]\" onClick=\"return confirm('Estas seguro de borrar?')\">Delete</a></td> </tr><br>";

            }
            /*
            while($res = mysqli_fetch_row($result)) {
                echo "<tr>";
                echo "<td>".$res['nombre']."</td>";
                echo "<td>".$res['fecha']."</td>";
                echo "<td>".$res['datos']."</td>";
                echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
            }
            */ ?>
            </tbody>
        </table>
    </div>
</div>
</html>

CONEXION.PHP

<?php
$conn = mysqli_connect("localhost", "root","","crud");
?>



<style>

    div.dataTables_wrapper {
        margin-bottom: 3em;
    }
</style>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css">  <!--se añaden librerias boostrap y datables para que quede más aseado-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>

<script>
    $(document).ready(function () {
        $('#example').DataTable();
    });
</script>

DELETE.PHP

<?php
require("conexion.php");

    $id = $_GET['id'];
mysqli_query($conn, "DELETE FROM tcrud WHERE id=$id");
header("Location: index.php");
?>

EDITAR.PHP

<?php
require("conexion.php");


if (isset($_POST['update'])) {
    $id = $_POST['id'];

    $nombre = $_POST['nombre'];
    $fecha = $_POST['fecha'];
    $dato = $_POST['dato'];


    // checking empty fields
    if (empty($nombre) || empty($fecha) || empty($dato)) {

        if (empty($nombre)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }

        if (empty($fecha)) {
            echo "<font color='red'>Quantity field is empty.</font><br/>";
        }

        if (empty($dato)) {
            echo "<font color='red'>Price field is empty.</font><br/>";
        }
    } else {
        //updating the table

        echo $id;
        echo $nombre;
        echo $fecha;
        echo $dato;
        $result = mysqli_query($conn, "UPDATE tcrud SET nombre='$nombre', fecha='$fecha', dato='$dato' WHERE id='$id'");
        print_r($result);
        //redirectig to the display page. In our case, it is view.php
        //header("Location: index.php");
    }
}
?>
<?php
if (isset($_GET['id'])) {
//getting id from url
    $id = $_GET['id'];
//selecting data associated with this particular id
    $result = mysqli_query($conn, "SELECT * FROM tcrud WHERE id='$id'");

    while ($res = mysqli_fetch_array($result)) {
        $nombre = $res['nombre'];
        $fecha = $res['fecha'];
        $dato = $res['dato'];
    }

}
?>
<html>
<head>
    <title>EditAR Data</title>
</head>

<body>
<div class="container">
    <div class="row">
        <a href="index.php">Home</a>

    </div>
    <form name="form1" method="post" action="edit.php">
        <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
            <thead>
            <tr>
                <th>OPCIONES</th>
                <th>DATOS</th>
            </tr>
            </thead>
            <tr>
                <td>NOMBRE</td>
                <td><input type="text" name="nombre" value="<?php echo $nombre; ?>"></td>
            </tr>
            <tr>
                <td>FECHA</td>
                <td><input type="text" name="fecha" value="<?php echo $fecha; ?>"></td>
            </tr>
            <tr>
                <td>DATOS</td>
                <td><input type="text" name="dato" value="<?php echo $dato; ?>"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $id; ?>></td>
                <td><input type="submit" name="update" value="ACTUALIZAR"></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

CREAR.PHP

<?php
require("conexion.php");


if(isset($_POST['create']))
{
    $nombre = $_POST['nombre'];
    $fecha = $_POST['fecha'];
    $dato = $_POST['dato'];


    // checking empty fields
    if(empty($nombre) || empty($fecha) || empty($dato)) {

        if(empty($nombre)) {
            echo "<font color='red'>Nombre es Vacio.</font><br/>";
        }

        if(empty($fecha)) {
            echo "<font color='red'>Quantity field is empty.</font><br/>";
        }

        if(empty($dato)) {
            echo "<font color='red'>Price field is empty.</font><br/>";
        }
    } else {
        echo $nombre;
        echo $nombre;
        echo $dato;
        $result = mysqli_query($conn, "INSERT INTO tcrud (nombre, fecha, dato) VALUES('$nombre','$fecha','$dato')");
        //updating the table
        //$result= mysqli_query($conn, "INSERT INTO tcrud ('$nombre', '$fecha', '$dato') VALUES ('$nombre', '$fecha', '$dato')");

        print_r($result);
        //redirectig to the display page. In our case, it is view.php
        //header("Location: index.php");
    }
}
?>
<html>
<head>
    <title>Edit Data</title>
</head>

<body>
<a href="index.php">Home</a>
<br/><br/>

<form name="form1" method="post" action="create.php">
    <table border="0">
        <tr>
            <td>Name</td>
            <td><input type="text" name="nombre"></td>
        </tr>
        <tr>
            <td>Quantity</td>
            <td><input type="text" name="fecha"></td>
        </tr>
        <tr>
            <td>Price</td>
            <td><input type="text" name="dato" ></td>
        </tr>
        <tr>
            <td><input type="hidden" name="id" ></td>
            <td><input type="submit" name="create"></td>
        </tr>
    </table>
</form>
</body>
</html>