Ver Mensaje Individual
  #6 (permalink)  
Antiguo 08/01/2016, 18:18
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Evitar eliminar registros con php

Lo adapté al código a otros delete y funciona lo más bien, ahora x_atrix quería preguntarte lo siguiente:

roles(rolId, rolNombre)

Tengo el rolNombre para ingresar y en la base de datos esta como clave única pero quería saber cómo hacer para mostrar un mensaje de: "El rol ya existe" tanto para guardar y editar, sólo que en el editar sucede que si no modifico el nombre y lo demás me puede salir ese mensaje.

Te muestro el código:

RolModel:

Código PHP:
Ver original
  1. public function guardame($rol){
  2.         $sql="insert into roles(rolNombre) values(?)";
  3.         $consulta = $this->getBD()->prepare($sql);
  4.         $consulta->execute(array($rol->getNombre()));
  5.         return ($consulta->rowCount() > 0) ? $this->getBD()->lastInsertId() : null;
  6.     }
  7.     public function modificame($rol){
  8.         $sql="update roles set rolNombre=? where rolId=?";
  9.         $consulta = $this->getBD()->prepare($sql);
  10.         $consulta->execute(array($rol->getNombre(),$rol->getId()));
  11.         return ($consulta->rowCount() > 0) ? $rol->getId() : null;
  12.     }

RolesController:

Código PHP:
Ver original
  1. public function add(){
  2.         if($this->checkUser()){
  3.             if (isset($_POST['btnaceptar'])) {
  4.                 if($this->checkDates()) {                
  5.                     $rol = new Rol(0, $_POST['txtnom']);
  6.                     $this->modelo->guardame($rol);
  7.                     Session::set("msg","Rol Creado");
  8.                     header("Location:index.php?c=roles&a=index");
  9.                     exit();
  10.                 }
  11.             }
  12.             $this->redirect(array('add.php'));
  13.         }
  14.     }  
  15.     public function edit(){        
  16.         if($this->checkUser()){
  17.             Session::set("id",$_GET['p']);
  18.             if (Session::get('id')!=null && isset($_POST['btnaceptar'])){                            
  19.                 if($this->checkDates()) {                
  20.                     $rol= new Rol($_POST['hid'],$_POST['txtnom']);
  21.                     $this->modelo->modificame($rol);
  22.                     Session::set("msg","Rol Editado");
  23.                     header("Location:index.php?c=roles&a=index");
  24.                     exit();
  25.                 }
  26.             }
  27.             $this->redirect(array('edit.php'),array(
  28.                 "rol" => $this->modelo->obtenerPorId(Session::get('id'))
  29.             ));
  30.         }        
  31.     }

Te lo pregunto xq me gustó mucho cómo me ayudaste con el borrar... Saludos y espero respuestas.