Ver Mensaje Individual
  #2 (permalink)  
Antiguo 07/07/2013, 07:07
rufus
 
Fecha de Ingreso: mayo-2009
Ubicación: Andalucia
Mensajes: 650
Antigüedad: 15 años, 10 meses
Puntos: 1
Respuesta: problema con insertar registro en mysql con php

y tambien esto:
Código PHP:
Ver original
  1. <?php
  2. require_once(LIB_DIR.SD."database.php");
  3.  
  4. class tabla
  5. {
  6.     protected static $nombre_tabla;
  7.     public static function buscar_por_id($id)
  8.     {
  9.         global $bd;
  10.         $matriz_usuarios = static::buscar_por_sql("SELECT * FROM " .static::$nombre_tabla."
  11.             WHERE id={$id}");
  12.        
  13.         return (!empty($matriz_usuarios)) ? array_shift($matriz_usuarios) : false;
  14.            
  15.     }
  16.    
  17.     public static function buscar_todos()
  18.     {
  19.        
  20.         return self::buscar_por_sql("SELECT * FROM". static::$nombre_tabla);
  21.     }
  22.    
  23.     public static function buscar_por_sql($sql)
  24.     {
  25.         global $bd;
  26.         $resultado = $bd->enviar_consulta($sql);
  27.         $matriz_usuarios = array();
  28.         while($registro = $bd->fetch_array($resultado))
  29.         {
  30.             array_push($matriz_usuarios, static::instanciar($registro));
  31.         }
  32.         return $matriz_usuarios;
  33.            
  34.     }
  35.    
  36.     public static function instanciar($registro)
  37.     {
  38.         $nombre_clase = get_called_class();
  39.         $objeto = new $nombre_clase;
  40.    
  41.    
  42.  
  43.         foreach($registro as $propiedad => $valor)
  44.         {
  45.             if($objeto->propiedad_existe($propiedad))
  46.             {
  47.                 $objeto->propiedad = $valor;
  48.             }
  49.         }
  50.         return $objeto;
  51.     }
  52.    
  53.     public function propiedad_existe($propiedad)
  54.     {
  55.         $propiedades =$this->propiedades();
  56.         return array_key_exists($propiedad,$propiedades);
  57.     }
  58.    
  59.    
  60.     public function propiedades()
  61.     {
  62.         return  get_object_vars($this);
  63.     }
  64.     public function crear()
  65.     {
  66.         global $bd;
  67.         $propiedades = $this->propiedades();
  68.         $sql = "INSERT INTO ".static::$nombre_tabla."(";
  69.         $sql .= implode(",",array_keys($propiedades));
  70.         $sql .=" ) VALUES ('";
  71.        
  72.         $sql .= implode("','", array_values($propiedades)). "')";
  73.         if($bd->enviar_consulta($sql))
  74.         {
  75.                 $this->id = $bd->insert_id();
  76.                 return true;
  77.  
  78.         }
  79.         else
  80.         {
  81.             return false;
  82.         }
  83.    
  84.     }
  85.    
  86.  
  87. /** public function insertarlibro()
  88.     {
  89.         global $bd;
  90.         $sql = "INSERT INTO libro(";
  91.         $sql .="autor, titulo, precio";
  92.         $sql .=") VALUES ('";
  93.         $sql .= $bd->preparar_consulta($this->autor) . "','";
  94.         $sql .= $bd->preparar_consulta($this->titulo) . "','";
  95.         $sql .= $bd->preparar_consulta($this->precio) .  "')";
  96.         $bd->enviar_consulta($sql);
  97.         $this->id = $bd->insert_id();
  98.     }
  99. */
  100.     public function actualizar()
  101.     {
  102.         global $bd;
  103.         $propiedades = $this->propiedades();
  104.         $prop_format = array();
  105.         foreach($propiedades as $propiedad => $valor)
  106.         {
  107.             array_push($prop_format,"{$propiedad}='{$valor}' ");
  108.  
  109.         }
  110.         $sql = "UPADTE ".static::$nombre_tabla." SET ";
  111.         $sql .= implode(",",$prop_format);
  112.         $sql .=" WERE id = ". $bd->preparar_consulta($this->id);
  113.         $bd->enviar_consulta($sql);
  114.         if($bd->affected_rows() == 1)
  115.         {
  116.             return true;
  117.         }
  118.         else
  119.         {
  120.             return false;
  121.         }
  122.     }
  123.    
  124.     public function eliminar()
  125.     {
  126.         global $bd;
  127.         $sql = "DELETE FROM ". static::$nombre_tabla;
  128.         $sql .= " WHERE ID =" . $bd->preparar_consulta($this->id);
  129.         $sql .= " LIMIT 1";
  130.         $bd->enviar_consulta($sql);
  131.         if($bd->affected_rows() == 1)
  132.         {
  133.             return true;
  134.         }
  135.         else
  136.         {
  137.             return false;
  138.         }
  139.     }
  140. }
  141.  
  142. ?>