I can not edit or delete the items in my database

0
<!DOCTYPE html> 
<meta charset="UTF-8">
<?php  
$con = mysqli_connect("localhost","root","gue55me","invensoft") or die("conexion exitosa!");
?>
<html>  
        <head>
            <meta chrset="UTF-8">
            <title>CRUD PHP & MySQL</title> 
        </head>
<body>

    <form method="POST" action="formulario.php">
        <label>Nombre:</label>
        <input type="text" name="nombre" placeholder="Escriba su nombre"/><br/>
        <label>Tipo:</label>
        <input type="text" name="tipo" placeholder="Escriba se contraseña"/><br/>
        <label>Fecha Vencimiento:</label>
        <input type="date" name="fecha" placeholder="Escriba la fecha"/><br/>
        <input type="submit" name="insert" value="INSERTAR DATOS"/>

    </form>


    <?php 
    if(isset($_POST['insert'])){


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

        $insertar = "INSERT INTO productos (prodnombre,prodtipo,fechavencimiento) values ('$nombre','$tipo','$fecha')";

        $ejecutar = mysqli_query($con,$insertar);

        if($ejecutar){

        echo "<h3>Insertado correctamente</h3>";
        }
    }

    ?> 
    <br/>
    <table width="500" border="2" style="background-color: #F9F9F9;">

        <tr>
            <th>ID</th>
            <th>Nombre</th>
            <th>Tipo</th>
            <th>Fecha Vencimiento</th>
            <th>Editar</th>
            <th>Borrar</th>
        </tr>

        <?php 


            $consulta = "SELECT * FROM productos";

            $ejecutar = mysqli_query($con, $consulta); 

            $i = 0;

            while($fila=mysqli_fetch_array($ejecutar)){         
                $id = $fila['idProducto'];
                $nombre = $fila['prodnombre']; 
                $tipo = $fila['prodtipo']; 
                $fecha = $fila['fechavencimiento'];

                $i++;   

        ?>
        <tr align="center">
            <td><?php echo $id; ?></td>
            <td><?php echo $nombre; ?></td>
            <td><?php echo $tipo; ?></td>
            <td><?php echo $fecha; ?></td>
            <td><a href="formulario.php?editar=<?php echo $id; ?>">Editar</a></td>
            <td><a href="formulario.php?borrar=<?php echo $id; ?>">Borrar</a></td>
        </tr>
        <?php } ?>


    </table>
    <?php
        if(isset($_GET['editar'])){
        include("editar.php");
        }
    ?> 
    <?php 
    if(isset($_GET['borrar'])){

    $borrar_id = $_GET['borrar'];

    $borrar = "DELETE FROM productos WHERE id='$borrar_id'";

    $ejecutar = mysqli_query($con,$borrar); 

        if($ejecutar){

        echo "<script>alert('El producto ha sido borrado!')</script>";
        echo "<script>window.open('formulario.php','_self')</script>";
        }

    }

    ?>


</body>
</html>

<?php 
        if(isset($_GET['editar'])){

            $editar_id = $_GET['editar']; 

            $consulta = "SELECT * FROM productos WHERE id='$editar_id'";
            $ejecutar = mysqli_query($con,$consulta); 

            $fila=mysqli_fetch_array($ejecutar);

            $nombre = $fila['prodnombre']; 
            $tipo = $fila['prodtipo']; 
            $fecha = $fila['fechavencimiento'];

            }
?>

<br/>
<form method="POST" action="">
        <input type="text" name="nombre" value="<?php echo $nombre;?>"/><br/>
        <input type="text" name="tipo" value="<?php echo $tipo;?>"/><br/>
        <input type="date" name="fecha" value="<?php echo $fecha ;?>"/><br/>
        <input type="submit" name="actualizar" value="ACTUALIZAR DATOS"/>

</form>
    <?php 
    if(isset($_POST['actualizar'])){

        $actualizar_nombre = $_POST['nombre'];
        $actualizar_tipo = $_POST['tipo'];
        $actualizar_fecha = $_POST['fecha'];

        $actualizar = "UPDATE productos SET prodnombre='$actualizar_nombre', prodtipo='$actualizar_tipo', fechavencimiento='$actualizar_fecha' WHERE id='$editar_id'";

        $ejecutar = mysqli_query($con, $actualizar);

        if($ejecutar){

        echo "<script>alert('Datos actualizados!')</script>";
        echo "<script>window.open('formulario.php','_self')</script>";
        }
    }

    ?>
    
asked by Ramses Bagett Sierra 12.04.2018 в 04:46
source

0 answers