Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/09/2018, 11:52
stevens82
 
Fecha de Ingreso: octubre-2011
Mensajes: 132
Antigüedad: 12 años, 6 meses
Puntos: 4
usar try catch con if else

Hola, necesito saber cual es la forma correcta de usar el try catch con un if else para unas consulats teniendo algo asi:

Código PHP:
Ver original
  1. public function insertarBlog($categoria, $titulo)
  2.     {  
  3.         if ($group_post) {
  4.         $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)";
  5.         $statement = $this->_db->prepare($sql);
  6.         $statement->execute(array(
  7.             ':id_categoria' => $categoria,
  8.             ':titulo'       => $titulo,
  9.         ));
  10.             if ($statement) {
  11.                 return true;
  12.             } else {
  13.                 return false;
  14.             }
  15.         } else {
  16.             $sql = "INSERT INTO posts (id, id_categoria, titulo, )";
  17.             $statement = $this->_db->prepare($sql);
  18.             $statement->execute(array(
  19.                 ':id_categoria' => $categoria,
  20.                 ':titulo'       => $titulo,
  21.             ));
  22.            if ($statement) {
  23.                 return true;
  24.             } else {
  25.                 return false;
  26.             }
  27.         }
  28.     }