Ver Mensaje Individual
  #1 (permalink)  
Antiguo 01/06/2018, 18:31
saulrayados
 
Fecha de Ingreso: abril-2016
Mensajes: 44
Antigüedad: 8 años
Puntos: 0
error al insertar a base de datos

necesito de su ayuda, al dar clic el boton guardar si guarda la imagen en la carpeta pero no guarda la informacion en la base de datos
la base de datos se llama galeria, la tabla se llama productos, los campos que tienen la tabla son: id, titulo, imagen, descripcion, fecha

subir.view.php
Código HTML:
Ver original
  1.     <meta charset="UTF-8">
  2.     <meta name="viewport" content="width=device-width, user-scalable=no,
  3.     initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  4. <link href="https://fonts.googleapis.com/css?family=Slabo+27px" rel="stylesheet">
  5. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
  6. <link rel="stylesheet" href="css/estilos.css">
  7. </head>
  8.     <header>
  9.     <div class="contenedor">
  10.         <h1 class="titulo">Subir Foto</h1>
  11.     </div>
  12.     </header>
  13.  
  14.     <div class="contenedor">
  15.         <form class="formulario" method="POST" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
  16.             <label for="foto">Selecciona tu foto</label>
  17.             <input type="file" id="foto" name="image">
  18.  
  19.             <label for="titulo">Titulo de la foto</label>
  20.             <input type="text" id="titulo" name="title">
  21.  
  22.             <label for="texto">Descripción:</label>
  23.             <textarea name="description" id="texto" placeholder="Ingresa una descripción"></textarea>
  24.  
  25.             <?php if (isset($error)): ?>
  26.                 <p class="error"><?php echo $error; ?></p>
  27.             <?php endif ?>
  28.  
  29.             <input type="submit" class="submit" value="Guardar">
  30.  
  31.         </form>
  32.     </div>
  33. </body>
  34. </html>

funciones.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. function conexion($db, $usuario, $pass){
  4.     try {
  5.         $conexion = new PDO("mysql:host=localhost;dbname=$db", $usuario, $pass);
  6.         return $conexion;
  7.     } catch (PDOException $e) {
  8.         return false;
  9.     }
  10. }
  11.  
  12. ?>

subir.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. require 'funciones.php';
  4. $conexion = conexion('galeria', 'root', '');
  5.  
  6. if (!$conexion) {
  7.     die();
  8. }
  9.  
  10. if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_FILES)) {
  11.     $check = @getimagesize($_FILES['image']['tmp_name']);
  12.     if ($check !== false) {
  13.         $carpeta_destino = 'fotos/';
  14.         $archivo_subido = $carpeta_destino . $_FILES['image']['name'];
  15.         move_uploaded_file($_FILES['image']['tmp_name'], $archivo_subido);
  16.  
  17.         $statement = $conexion->prepare('
  18.             INSERT INTO productos (id, titulo, imagen, descripcion)
  19.             VALUES (:title, :image, :description)
  20.         ');
  21.  
  22.         $statement->execute(array(
  23.             ':title' => $_POST['titulo'],
  24.             ':image' => $_FILES['imagen']['name'],
  25.             ':description' => $_POST['descripcion']
  26.         ));
  27.  
  28.         header('Location: index.php');
  29.     } else {
  30.         $error = "El archivo no es una imagen o el archivo es muy pesado";
  31.     }
  32. }
  33.  
  34. require 'views/subir.view.php';
  35.  
  36. ?>