Ver Mensaje Individual
  #7 (permalink)  
Antiguo 26/10/2011, 07:09
ofertasdiarias
 
Fecha de Ingreso: marzo-2011
Mensajes: 115
Antigüedad: 13 años, 2 meses
Puntos: 0
Respuesta: Como insertar imagenes con formulario a una base de datos

la base de datos
Código MySQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `formulario` (
  2.   `id_rubro` int(11) NOT NULL AUTO_INCREMENT,
  3.   `Imagen` varchar(255) NOT NULL,
  4.   PRIMARY KEY (`id_rubro`)

yo tengo este formulario.php

Código PHP:
Ver original
  1. [COLOR="Red"]<?php require_once('Connections/localhost.php'); ?>
  2. <?php[/COLOR][COLOR="Blue"]esta parte la modificas con los datos de tu coneccion[/COLOR]
  3. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  4. {
  5.   $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
  6.  
  7.   switch ($theType) {
  8.     case "text":
  9.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  10.       break;    
  11.     case "long":
  12.     case "int":
  13.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  14.       break;
  15.     case "double":
  16.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  17.       break;
  18.     case "date":
  19.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  20.       break;
  21.     case "defined":
  22.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  23.       break;
  24.   }
  25.   return $theValue;
  26. }
  27.  
  28. $editFormAction = $_SERVER['PHP_SELF'];
  29. if (isset($_SERVER['QUERY_STRING'])) {
  30.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  31. }
  32.  
  33. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  34.     [COLOR="Red"]//Guardar imagen
  35.     if(is_uploaded_file($_FILES['Imagen']['tmp_name'])) { // verifica haya sido cargado el archivo
  36.     $ruta= "imagenes/".$_FILES['Imagen']['name'];
  37.     move_uploaded_file($_FILES['Imagen']['tmp_name'], $ruta);
  38.     }[/COLOR] [COLOR="Blue"]como vez esta parte es la que tienes que modificar con el nombre de tu carpeta y el nombre del formulario[/COLOR]
  39.   $insertSQL = sprintf("INSERT INTO formulario (Imagen) VALUES (%s)",
  40.                        GetSQLValueString($ruta, "text"));
  41.  
  42.   mysql_select_db($database_localhost, $localhost);
  43.   $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
  44.  
  45.   $insertGoTo = "ingreso_exisoto.php";
  46.   if (isset($_SERVER['QUERY_STRING'])) {
  47.     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  48.     $insertGoTo .= $_SERVER['QUERY_STRING'];
  49.   }
  50.   header(sprintf("Location: %s", $insertGoTo));
  51. }
  52. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  53. <html xmlns="http://www.w3.org/1999/xhtml">
  54. <head>
  55. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  56. <title>Documento sin t&iacute;tulo</title>
  57. <style type="text/css">
  58. <!--
  59. #Layer1 {
  60.     position:absolute;
  61.     left:137px;
  62.     top:85px;
  63.     width:500px;
  64.     height:500px;
  65.     z-index:1;
  66. }
  67. -->
  68. </style>
  69. </head>
  70.  
  71. <body>
  72. <div id="Layer1">
  73.   <form id="form1" name="form1" enctype="multipart/form-data" method="POST" action="<?php echo $editFormAction; ?>">
  74.     <label for="file"></label>
  75.     <p>
  76.       <input type="file" name="Imagen" id="Imagen" />
  77.       <label></label>
  78.     </p>
  79.     <label for="Submit"></label>
  80.     <input type="submit" name="Submit" value="Enviar" id="Submit" />
  81.     <input type="hidden" name="MM_insert" value="form1">
  82.   </form>
  83. </div>
  84. </body>
  85. </html>

y este es el ingreso_exitoso.php

Código PHP:
Ver original
  1. <?php require_once('Connections/localhost.php'); ?>
  2. <?php
  3. $maxRows_Recordset1 = 5;
  4. $pageNum_Recordset1 = 0;
  5. if (isset($_GET['pageNum_Recordset1'])) {
  6.   $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
  7. }
  8. $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
  9.  
  10. mysql_select_db($database_localhost, $localhost);
  11. $query_Recordset1 = "SELECT * FROM formulario";
  12. $query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
  13. $Recordset1 = mysql_query($query_limit_Recordset1, $localhost) or die(mysql_error());
  14. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  15.  
  16. if (isset($_GET['totalRows_Recordset1'])) {
  17.   $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
  18. } else {
  19.   $all_Recordset1 = mysql_query($query_Recordset1);
  20.   $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
  21. }
  22. $totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
  23. ?>
  24. <table border="0" cellpadding="0">
  25.   <?php do { ?>
  26.     <tr>
  27.       <td>&nbsp;</td>
  28.       <td><a href="<?php echo $row_Recordset1['Imagen']; ?>"><img src="<?php echo $row_Recordset1['Imagen']; ?>" alt="" width="50" height="50" border="0" lowsrc="imagenes/168483_491704137718_788302718_5827885_3549642_n.jpg"></a></td>
  29.     </tr>
  30.     <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
  31. </table>
  32. <?php
  33. mysql_free_result($Recordset1);
  34. ?>


espero que te sea util yo lo utilizo ien mi sitio que estoy haciendo y funciona de maravillas. cualquier consulta o duda a la orden

este script es con dreamweaver!!