<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Mantenimiento categorías</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php
require_once 'libreria.php';
$nombre = filter_input(INPUT_GET, 'nombre', FILTER_SANITIZE_STRING);
$id = filter_input(INPUT_GET, 'idcategoria', FILTER_VALIDATE_INT);
if (!empty($nombre)) {
insertarCategoria($nombre);
}
if (!empty($id)) {
borrarCategoria($id);
}
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$nombre = filter_input(INPUT_GET, 'categoria', FILTER_SANITIZE_STRING);
if (!empty($id) && !empty($nombre)) {
editarCategoria($id, $nombre);
}
?>
<h1>Mantenimiento categorías</h1>
<form>Nombre: <input type="text" name="nombre"><input type="submit" class="btn btn-success"></form>
<hr/>
<?php
listadoCategorias();
?>
</body>
</html>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Editar categoría</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php
require_once 'libreria.php';
$id = filter_input(INPUT_GET, 'idcategoria', FILTER_VALIDATE_INT);
if(!empty($id)){
$categoria=getCategoria($id);
}
?>
<form action="categorias.php">
<input type="hidden" name="id" value="<?=$categoria['idcategorias']?>">
Nombre: <input type="text" name="categoria" value="<?=$categoria['nombre']?>">
<input type="submit" class="btn btn-success">
</form>
<a href="categorias.php">Volver a categorías</a>
</body>
</html>
<?php
function conectar() {
$server = "localhost";
$user = "root";
$password = "";
$db = "tienda";
try {
$conn = new PDO("mysql:host=$server;dbname=$db", $user, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function insertarCategoria($name) {
try {
$conn = conectar();
$st = $conn->prepare("insert into categorias (nombre) values (:name)");
$st->execute(['name' => $name]);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function borrarCategoria($id) {
try {
$conn = conectar();
$st = $conn->prepare("delete from categorias where idcategorias=:id");
$st->execute(['id' => $id]);
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function editarCategoria($id,$nombre){
try{
$conn=conectar();
$st=$conn->prepare("update categorias set nombre=:nombre where idcategorias=:id");
$st->execute(['nombre'=>$nombre,'id'=>$id]);
} catch (Exception $ex) {
}
}
function getCategorias() {
try {
$conn = conectar();
$st = $conn->prepare("select * from categorias");
$st->execute();
$categorias = $st->fetchAll();
return $categorias;
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function getCategoria($id) {
try {
$conn = conectar();
$st = $conn->prepare("select * from categorias where idcategorias=:id");
$st->execute(['id' => $id]);
$categoria = $st->fetch();
return $categoria;
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
function listadoCategorias() {
$categorias = getCategorias();
?>
<table class="table">
<tr><td>Id</td><td>Nombre</td><td>Acciones</td></tr>
<?php
foreach ($categorias as $categoria) {
?>
<tr><td><?= $categoria['idcategorias'] ?></td>
<td><?= $categoria['nombre'] ?></td>
<td>
<a href="?idcategoria=<?= $categoria['idcategorias'] ?>">Borrar</a>
<a href="editar_categoria.php?idcategoria=<?= $categoria['idcategorias'] ?>">Editar</a>
</td>
</tr>
<?php
}
?>
</table>
<?php
}