Foros del Web » Programando para Internet » PHP »

problema con insertar registro en mysql con php

Estas en el tema de problema con insertar registro en mysql con php en el foro de PHP en Foros del Web. que tengo una tabla = "foto" con llos campos id,archivo,tipo,peso,titulo. pero no se por que se me agregan dos campos mas(dos arrays de errores). aqui ...
  #1 (permalink)  
Antiguo 07/07/2013, 07:00
 
Fecha de Ingreso: mayo-2009
Ubicación: Andalucia
Mensajes: 650
Antigüedad: 15 años
Puntos: 1
problema con insertar registro en mysql con php

que tengo una tabla = "foto" con llos campos id,archivo,tipo,peso,titulo.

pero no se por que se me agregan dos campos mas(dos arrays de errores).

aqui el error: No se ha podido realizar la consultaUnknown column 'errores' in 'field list'
Ultima consulta sql:INSERT INTO foto(id,archivo,tipo,peso,titulo,errores,errores_u pload ) VALUES ('','Hydrangeas.jpg','image/jpeg','595284','','Array','Array')

php del formulario:

Código PHP:
Ver original
  1. <form action="formulario.php" method="post" enctype="multipart/form-data">
  2.         <table>
  3.         <!--     <tr>
  4.                 <td>autor:</td>
  5.                 <td><input type="text" name="autor" /></td>
  6.             </tr>
  7.             <tr>
  8.                 <td>titulo:</td>
  9.                 <td><input type="text" name="titulo" /></td>
  10.             </tr>
  11.             <tr>
  12.                 <td>precio:</td>
  13.                 <td><input type="text" name="precio" /></td>
  14.             </tr>
  15.  -->
  16.              <tr>
  17.              
  18.                 <td><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /></td>
  19.             </tr>
  20.  
  21.                <tr>
  22.                 <td>archivo:</td>
  23.                 <td><input type="file" name="file_upload"  /></td>
  24.             </tr>
  25.  
  26.  
  27.             <tr>
  28.                 <td>titulo imagen</td>
  29.                 <td><input type="text" name="titulo" value="" /></td>
  30.             </tr>
  31.  
  32.         </table>
  33.         <input type="submit" name="submit" value="Ingresar" />
  34.         </form>
  35.      </td>
  36.     </tr>
  37.   </table>

codigo php que procesa el insertado.

Código PHP:
Ver original
  1. <?php
  2. class Foto extends tabla
  3. {
  4.  
  5.    
  6.     public $id;
  7.     public $archivo;
  8.     public $tipo;
  9.     public $peso;
  10.     public $titulo;
  11.     private $nombre_tmp;
  12.     private $fotos_dir ="images";
  13.  
  14.     public $errores = array();
  15.     public $errores_upload = array(
  16.             UPLOAD_ERR_OK => "no se ha producido ningún error",
  17.             UPLOAD_ERR_INI_SIZE =>"El tamaño de archivo ha excedido el maximo indicando en php.ini",
  18.             UPLOAD_ERR_FORM_SIZE => "El tamañode archivo ha excedido el maximo para este formulario",
  19.             UPLOAD_ERR_PARTIAL => "El archivo ha sido subido parcialmente",
  20.             UPLOAD_ERR_NO_FILE=> "el archivo no existe",
  21.             UPLOAD_ERR_NO_TMP_DIR=>"El directorio temporal no existe",
  22.             UPLOAD_ERR_CANT_WRITE=> "No se puede escribir en el disco duro",
  23.             UPLOAD_ERR_EXTENSION=> "Error en una extensión php");
  24.  
  25.  
  26.     protected static $nombre_tabla = "foto";
  27.     protected static $campos_tabla = array("archivo", "tipo","peso","titulo");
  28.  
  29.  
  30.     public function adjuntar_foto($info)
  31.     {
  32.         if(!$info || empty($info) || !is_array($info))
  33.         {
  34.             array_push($errores,"no se a pasado ninguna informacion de archivo.");
  35.             return false;
  36.  
  37.         }
  38.         elseif ($info["error"] != 0)
  39.         {
  40.             array_push($errores,$errores_upload[$info["error"]]);
  41.             return false;
  42.         }
  43.         else
  44.         {
  45.         $this->archivo = basename($info["name"]);
  46.         $this->peso = $info["size"];
  47.         $this->tipo = $info["type"];
  48.         $this->nombre_tmp = $info["tmp_name"];
  49.         return true;
  50.        
  51.         }
  52.     }
  53.  
  54.  
  55.  
  56.     public function guardar()
  57.         {
  58.             if(!isset($this->id))
  59.             {
  60.                 if(!empty($this->errores))
  61.                 {
  62.                     return false;
  63.                 }
  64.  
  65.                 if(strlen($this->titulo) > 255)
  66.                 {
  67.                     $this->errores[] = "El titulo posee más de 255 caracteres";
  68.                     return false;
  69.                 }
  70.                 $nueva_ruta = RAIZ_DIR.SD."public".SD.$this->fotos_dir.SD.$this->archivo;
  71.  
  72.                 if(empty($this->nombre_tmp))
  73.                 {
  74.                     $this->errores[] = "No se tienen los datos suficientes";
  75.                     return false;
  76.  
  77.                 }
  78.  
  79.                 if(file_exists($nueva_ruta))
  80.                 {
  81.                     $this->errores[] = "No se puede utilizar ese nombre de archivo";
  82.                     return false;
  83.                 }
  84.                 if(move_uploaded_file($this->nombre_tmp,$nueva_ruta))
  85.                 {
  86.                     if($this->crear())
  87.                     {
  88.                         return true;
  89.  
  90.                     }
  91.                     else
  92.                     {
  93.                         return false;
  94.                         $this->errores[ ] = "No se ha podido crear el registro en la base de datos";
  95.                     }
  96.                 }
  97.                     else
  98.                     {
  99.                         $this->errores[] = "No se ha podido mover el archivo subido a una ubicación segura.";
  100.                         return false;
  101.                     }
  102.                
  103.            
  104.                }
  105.             else
  106.             {
  107.                 $this->actualizar();
  108.             }
  109.         }
  110.    
  111.  
  112. }
  113.  
  114. ?>
  #2 (permalink)  
Antiguo 07/07/2013, 07:07
 
Fecha de Ingreso: mayo-2009
Ubicación: Andalucia
Mensajes: 650
Antigüedad: 15 años
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. ?>
  #3 (permalink)  
Antiguo 08/07/2013, 01:29
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: problema con insertar registro en mysql con php

Unknown column 'errores' in 'field list' significa que ese campo no existe en la tabla.
Revisa tu código linea por línea, también tienes errores como en esta parte:

Código PHP:
Ver original
  1. $sql = "UPADTE ".static::$nombre_tabla." SET ";
  2.         $sql .= implode(",",$prop_format);
  3.         $sql .=" WERE id = ". $bd->preparar_consulta($this->id);

Salu2
__________________
Fere libenter homines, id quod volunt, credunt.
  #4 (permalink)  
Antiguo 08/07/2013, 04:08
 
Fecha de Ingreso: mayo-2009
Ubicación: Andalucia
Mensajes: 650
Antigüedad: 15 años
Puntos: 1
Respuesta: problema con insertar registro en mysql con php

gracias ya he visto el error era la clausula "where".

pero ahora me da dos errores el mismo formulario.

( ! ) Notice: Undefined variable: errores_upload in E:\wamp\www\rayuela2\includes\photo.php on line 44
Call Stack
# Time Memory Function Location
1 0.0005 685072 {main}( ) ..\formulario.php:0
2 0.0057 866096 Foto->adjuntar_foto( ) ..\formulario.php:27

( ! ) Warning: array_push() expects parameter 1 to be array, null given in E:\wamp\www\rayuela2\includes\photo.php on line 44
Call Stack
# Time Memory Function Location
1 0.0005 685072 {main}( ) ..\formulario.php:0
2 0.0057 866096 Foto->adjuntar_foto( ) ..\formulario.php:27
3 0.0060 866240 array_push ( ) ..\photo.php:44

Código PHP:
Ver original
  1. elseif ($info["error"] != 0)
  2.         {
  3. //esta el error  array_push($errores,$errores_upload[$info["error"]]);
  4.             return false;
  5.         }

no se por que me dice que esta la variable indefenida cuando esta declarda mas arriba.

Código PHP:
Ver original
  1. <?php
  2. require_once(LIB_DIR.SD."initialize.php");
  3. ?>
  4.  
  5. <?php
  6. class Foto extends tabla
  7. {
  8.  
  9.    
  10.     public $id;
  11.     public $archivo;
  12.     public $tipo;
  13.     public $peso;
  14.     public $titulo;
  15.     private $nombre_tmp;
  16.     private $fotos_dir ="images";
  17.  
  18.     public $errores = array();
  19.     public $errores_upload = array(
  20.             UPLOAD_ERR_OK => "no se ha producido ningún error",
  21.             UPLOAD_ERR_INI_SIZE =>"El tamaño de archivo ha excedido el maximo indicando en php.ini",
  22.             UPLOAD_ERR_FORM_SIZE => "El tamañode archivo ha excedido el maximo para este formulario",
  23.             UPLOAD_ERR_PARTIAL => "El archivo ha sido subido parcialmente",
  24.             UPLOAD_ERR_NO_FILE=> "el archivo no existe",
  25.             UPLOAD_ERR_NO_TMP_DIR=>"El directorio temporal no existe",
  26.             UPLOAD_ERR_CANT_WRITE=> "No se puede escribir en el disco duro",
  27.             UPLOAD_ERR_EXTENSION=> "Error en una extensión php");
  28.  
  29.  
  30.     protected static $nombre_tabla = "foto";
  31.     protected static $campos_tabla = array("archivo", "tipo","peso","titulo");
  32.  
  33.  
  34.     public function adjuntar_foto($info)
  35.     {
  36.         if(!$info || empty($info) || !is_array($info))
  37.         {
  38.             array_push($errores,"no se a pasado ninguna informacion de archivo.");
  39.             return false;
  40.  
  41.         }
  42.         elseif ($info["error"] != 0)
  43.         {
  44.             array_push($errores,$errores_upload[$info["error"]]);
  45.             return false;
  46.         }
  47.         else
  48.         {
  49.         $this->archivo = basename($info["name"]);
  50.         $this->peso = $info["size"];
  51.         $this->tipo = $info["type"];
  52.         $this->nombre_tmp = $info["tmp_name"];
  53.         return true;
  54.        
  55.         }
  56.     }
  57.  
  58.  
  59.  
  60.     public function guardar()
  61.         {
  62.             if(!isset($this->id))
  63.             {
  64.                 if(!empty($this->errores))
  65.                 {
  66.                     return false;
  67.                 }
  68.  
  69.                 if(strlen($this->titulo) > 255)
  70.                 {
  71.                     $this->errores[] = "El titulo posee más de 255 caracteres";
  72.                     return false;
  73.                 }
  74.                 $nueva_ruta = RAIZ_DIR.SD."public".SD.$this->fotos_dir.SD.$this->archivo;
  75.  
  76.                 if(empty($this->nombre_tmp))
  77.                 {
  78.                     $this->errores[] = "No se tienen los datos suficientes";
  79.                     return false;
  80.  
  81.                 }
  82.  
  83.                 if(file_exists($nueva_ruta))
  84.                 {
  85.                     $this->errores[] = "No se puede utilizar ese nombre de archivo";
  86.                     return false;
  87.                 }
  88.                 if(move_uploaded_file($this->nombre_tmp,$nueva_ruta))
  89.                 {
  90.                     if($this->crear())
  91.                     {
  92.                         return true;
  93.  
  94.                     }
  95.                     else
  96.                     {
  97.                         return false;
  98.                         $this->errores[ ] = "No se ha podido crear el registro en la base de datos";
  99.                     }
  100.                 }
  101.                     else
  102.                     {
  103.                         $this->errores[] = "No se ha podido mover el archivo subido a una ubicación segura.";
  104.                         return false;
  105.                     }
  106.                
  107.            
  108.                }
  109.             else
  110.             {
  111.                 $this->actualizar();
  112.             }
  113.         }
  114.        
  115.  
  116.  
  117.  
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124. ?>

Etiquetas: formulario, mysql, registro, sql, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:34.