Change primary key through a web interface made by PDO

0

I have created a database with 7 tables, including all of them related.

In this web interface I need to modify all the records of the table including the field of the primary key, but I am never able to edit it and when I approach it it tells me that it is duplicated.

I have attached a mega link with my sql and my php where I have everything to modify it or I do not know what you want it to happen to you.

link

This is the code:

Index.php

    <?php
require_once 'alumno.entidad.php';
require_once 'alumno.model.php';

// Logica
$alm = new Categoria();
$model = new CategoriaModel();

if(isset($_REQUEST['action']))
{
    switch($_REQUEST['action'])
    {
        case 'actualizar':

            // Recupero el acronimo del campo hidden

            $alm->__SET('acronimo',   $_REQUEST['acronimo']);
            $alm->__SET('categoria',  $_REQUEST['categoria']);

            $update_results = $model->Actualizar($alm);
            header('Location: index.php');
            break;

        case 'registrar':
            $alm->__SET('acronimo',  $_REQUEST['acronimo']);
            $alm->__SET('categoria', $_REQUEST['categoria']);

            $model->Registrar($alm);
            header('Location: index.php');
            break;

       case 'eliminar':
            $model->Eliminar($_REQUEST['acronimo']);
            header('Location: index.php');
            break;

        case 'editar':
            // Recupero los datos por el acronimo
            // $obj_categoria es un objeto del tipo Categoria
            $obj_categoria = $model->Obtener($_REQUEST['acronimo']);
            break;
        default: 
            // MENSAJE 404 PARA CUANDO LA ACCION NO ES VALIDA
            header('HTTP/1.0: 404 Not Found');
            die('<h1>404 Page Not Found</h1>');
    }
} 

?>

<!DOCTYPE html>
<html lang="es">
    <head>
        <title>Anexsoft</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <meta name="viewport" content="width=device-width, user-scalable=no">
    </head>
    <body style="padding:15px;">

        <div class="table table-striped">
            <div class="pure-u-1-12">

                <form action="?action=<?php echo isset($obj_categoria) ? 'actualizar' : 'registrar'; ?>" method="POST" class="pure-form pure-form-stacked" style="margin-bottom:30px;">
                    <input type="hidden" name="acronimo" value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acronimo') : ''; ?>" />

                    <table style="width:500px;">
                        <tr>
                            <th style="text-align:left;">Acronimo</th>
                            <td><input type="text" name="acronimo"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acronimo') : ''; ?>" style="width:100%;" required /></td>
                        </tr>
                        <tr>
                            <th style="text-align:left;">Categoria</th>
                            <td><input type="text" name="categoria"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('categoria') : ''; ?>" style="width:100%;" required/></td>
                            <td colspan="2">
                                 <button type="submit" class="btn btn-primary">Guardar</button>
                            </td>
                        </tr>
                    </table>
                </form>

                <table class="pure-table pure-table-horizontal">
                    <thead>
                        <tr>
                            <th style="text-align:left;">Acronimo</th>
                            <th style="text-align:left;">Categoria</th>

                        </tr>
                    </thead>
                    <?php foreach($model->Listar() as $r): ?>
                        <tr>
                            <td><?php echo $r->__GET('acronimo'); ?></td>
                            <td><?php echo $r->__GET('categoria'); ?></td>

                            <td>
                                <a href="?action=editar&acronimo=<?php echo $r->acronimo; ?>">Editar</a>
                            </td>
                            <td>
                                <a href="?action=eliminar&acronimo=<?php echo $r->acronimo; ?>">Eliminar</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </table>

            </div>
        </div>
          <script>js/jquery.js</script>
          <script>js/bootstrap.min.js</script>
    </body>
</html>

student.entity.php

<?php
class Categoria
{
    private $acronimo;
    private $categoria;

    public function __GET($k){ return $this->$k; }
    public function __SET($k, $v){ return $this->$k = $v; }
}


alumno.model.php

<?php
class CategoriaModel
{
    private $pdo;

    public function __CONSTRUCT()
    {
        try
        {
            $this->pdo = new PDO('mysql:host=localhost;dbname=deimos1', 'root', '');
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }

    public function Listar()
    {
        try
        {
            $result = array();

            $stm = $this->pdo->prepare("SELECT * FROM categoria");
            $stm->execute();

            foreach($stm->fetchAll(PDO::FETCH_OBJ) as $r)
            {
                $alm = new Categoria();

                $alm->__SET('acronimo', $r->acronimo);
                $alm->__SET('categoria', $r->categoria);
                $result[] = $alm;
            }

            return $result;
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }

    public function Obtener($acronimo)
    {
        try
        {
            $stm = $this->pdo->prepare("SELECT * FROM categoria WHERE acronimo = ?");

            $stm->execute(array($acronimo));
            $r = $stm->fetch(PDO::FETCH_OBJ);

            $alm = new Categoria();

            $alm->__SET('acronimo', $r->acronimo);
            $alm->__SET('categoria', $r->categoria);

            return $alm;
        } catch (Exception $e)
        {
            die($e->getMessage());
        }
    }

    public function Eliminar($acronimo)
    {
        try
        {
            $stm = $this->pdo
                      ->prepare("DELETE FROM categoria WHERE acronimo = ?");

            $stm->execute(array($acronimo));
        } catch (Exception $e)
        {
            die($e->getMessage());
        }
    }

    // ##############################################################################################
    // CAMBIOS REALIZADOS
    // - USO DE PARAMETROS NOMBRADOS EN EN METODO .execute()
    // ##############################################################################################       
    public function Actualizar(Categoria $data)
    {
        try
        {
            $sql = "UPDATE categoria SET acronimo =:acronimo, categoria =:categoria WHERE acronimo=acronimo";

            return $this->pdo->prepare($sql)
                 ->execute(
                    array(
                        ':acronimo' => $data->__GET('acronimo'),
                        ':categoria' => $data->__GET('categoria')
                    )
                );
        } catch (Exception $e)
        {
            die($e->getMessage());
        }
    }

    // ##############################################################################################

    public function Registrar(Categoria $data)
    {
        try
        {
        $sql = "INSERT INTO categoria (acronimo,categoria)
                VALUES (?, ?)";

        $this->pdo->prepare($sql)
             ->execute(
            array(
                $data->__GET('acronimo'),
                $data->__GET('categoria')
                )
            );
        } catch (Exception $e)
        {
            die($e->getMessage());
        }
    }
}
    
asked by Alberto Cepero de Andrés 03.04.2017 в 11:03
source

0 answers