Ver Mensaje Individual
  #4 (permalink)  
Antiguo 17/09/2018, 10:02
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 10 meses
Puntos: 379
Respuesta: usar try catch con if else

Es sencillo, puedes hacer algo como
Código PHP:
Ver original
  1. try {
  2.        if ($group_post) {
  3.         $sql = "INSERT INTO posts (id, id_categoria, titulo, contenido, imagen, estado, slug, id_parent, id_lang, fecha_registro, fecha_edicion, id_usuario, group_post) VALUES (null, :id_categoria, :titulo, :contenido, :imagen, 1, :slug, :id_parent, :id_lang, now(), now(), '" . Session::get('id_usuario') . "', :group_post)";
  4.         $statement = $this->_db->prepare($sql);
  5.         $statement->execute(array(
  6.             ':id_categoria' => $categoria,
  7.             ':titulo'       => $titulo,
  8.         ));
  9.            
  10.         } else {
  11.             $sql = "INSERT INTO posts (id, id_categoria, titulo, )";
  12.             $statement = $this->_db->prepare($sql);
  13.             $statement->execute(array(
  14.                 ':id_categoria' => $categoria,
  15.                 ':titulo'       => $titulo,
  16.             ));
  17.          
  18.         }
  19.            
  20.     } catch (PDOException $exception) {
  21.        //Manejas excepción si falla algo en el insert
  22.     } catch (Exception $exception) {
  23.         //Manejas excepción si sucede algo mas
  24.     }
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.