naquo

Baja TP Agus

Nov 18th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <html>
  2. <head>
  3.   <title>baja_usuario</title>
  4. </head>
  5. <body>
  6.   <?php
  7.   function connect_to_db($host = "localhost", $username = "root", $password = "", $dbname = "juralfa") {
  8.     $conexion = mysql_connect("localhost", "root", "")
  9.       or die("Problemas en la conexion");
  10.  
  11.     mysql_select_db("juralfa", $conexion)
  12.       or die("Problemas en la selección de la base de datos");
  13.  
  14.     return $conexion;
  15.   }
  16.  
  17.   function generic_query($type, $column, $table, $id = NULL) {
  18.     // This should be in a configuration file or an .php.inc
  19.     $templates = array(
  20.       'select' => 'SELECT %1$s FROM %2$s WHERE %1$s = %3$s',
  21.       'delete' => 'DELETE FROM %2$s WHERE %1$s = %3$s'
  22.     );
  23.     return sprintf($templates[$type], $column, $table, $id);
  24.   }
  25.  
  26.   /*
  27.    * El array está en este formato:
  28.    *   'código' => array(nombretabla, columnaPK)
  29.    */
  30.   $schema = array(
  31.     'propietario' => array('propietario', 'id_propietario'),
  32.     'apiario' => array('apiarios', 'id_apiario'),
  33.     'colmena' => array('colmena', 'id_colmena'),
  34.     'producto' => array('productos', 'id_producto'),
  35.     'registro' => array('registros_salud', 'id_registro')
  36.   );
  37.  
  38.   public function baja($entidad, $id) {
  39.     $conexion = connect_to_db();
  40.     $tabla = $schema[$entidad][0];
  41.     $columna = $schema[$entidad][1];
  42.  
  43.  
  44.     $select_query = generic_query('select', $tabla, $columna, $id);
  45.     $registros = mysql_query($select_query, $conexion)
  46.       or die('Problemas en el select: ' . mysql_error());
  47.    
  48.     if($reg = mysql_fetch_array($registros)) {
  49.       $delete_query = generic_query('delete', $tabla, $columna, $id);
  50.       mysql_query($delete_query, $conexion)
  51.         or die('Problemas en el delete: ' . mysql_error());
  52.       echo "Se efectuo el borrado del propietario con dicho codigo";
  53.     } else {
  54.       echo "No existe un usuario con ese id.";
  55.     }
  56.     mysql_close($conexion);
  57.   }
  58.  
  59.   baja($_GET['entidad'], $_GET['id']);
  60. ?>
  61. </body>
  62. </html>
Advertisement
Add Comment
Please, Sign In to add comment