Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/01/2013, 04:22
ae11
 
Fecha de Ingreso: enero-2013
Mensajes: 3
Antigüedad: 11 años, 4 meses
Puntos: 0
Exclamación Modificar formulario de insercion para subir imagen

Buenos días,

Agradecería muchísimo si alguien puede ayudarme. He estado buscando, probando... y no soy capaz de hacer que funcione!!!

Tengo una página de inserción de registros. Necesito que el campo 'foto' tenga un campo de archivo para poder subirla a una carpeta del servidor y a la vez poder almacenar la ruta en en el campo 'foto' de la tabla de productos, donde se almacenan el resto de los datos del formulario.

Con el código que tengo, puedo subir la imagen a la carpeta del servidor, pero no almacena la ruta en el campo de la base de datos (se queda vacio).

He probado con un formulario sencillo, solo con el campo para archivo y enviarlo a una tabla solo para las fotos y funciona perfectamente. El problema es que no soy capaz de implementar el codigo para que funcione con mi formulario...

Alguien puede ayudarme??? ya no se que hacer...

Mil gracias por vuestro tiempo y por cualquier tipo de ayuda!!!


El codigo de mi pagina para insertar registro es:

Código:
<?php include ('procesar.php');?>
<?php require_once('Connections/prueba.php'); mysql_query("SET NAMES 'utf8'");?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
  $insertSQL = sprintf("INSERT INTO tproducto (cod_barras, nombre, familia, subfamilia, genero, edad, descripcion, publicar, novedad, cantidad, precio) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", 
                       GetSQLValueString($_POST['cod_barras'], "text"),
                       GetSQLValueString($_POST['nombre'], "text"),
                       GetSQLValueString($_POST['familia'], "int"),
                       GetSQLValueString($_POST['subfamilia'], "int"),
                       GetSQLValueString($_POST['genero'], "int"),
                       GetSQLValueString($_POST['edad'], "int"),
                       GetSQLValueString($_POST['descripcion'], "text"),
                       GetSQLValueString(isset($_POST['publicar']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString(isset($_POST['novedad']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['cantidad'], "int"),
                       GetSQLValueString($_POST['precio'], "double"));

  mysql_select_db($database_prueba $prueba);
  $Result1 = mysql_query($insertSQL, $prueba) or die(mysql_error());
}

mysql_select_db($database_prueba, $prueba);
$query_rs_familia = "SELECT * FROM tfamilia";
$rs_familia = mysql_query($query_rs_familia, $prueba) or die(mysql_error());
$row_rs_familia = mysql_fetch_assoc($rs_familia);
$totalRows_rs_familia = mysql_num_rows($rs_familia);

mysql_select_db($database_prueba, $prueba);
$query_rsgenero = "SELECT * FROM tgenero";
$rsgenero = mysql_query($query_rsgenero, $prueba) or die(mysql_error());
$row_rsgenero = mysql_fetch_assoc($rsgenero);
$totalRows_rsgenero = mysql_num_rows($rsgenero);

mysql_select_db($database_prueba, $prueba);
$query_rssubfamilia = "SELECT * FROM tsubfamilia";
$rssubfamilia = mysql_query($query_rssubfamilia, $prueba) or die(mysql_error());
$row_rssubfamilia = mysql_fetch_assoc($rssubfamilia);
$totalRows_rssubfamilia = mysql_num_rows($rssubfamilia);

mysql_select_db($database_prueba, $prueba);
$query_rsedades = "SELECT * FROM tedades";
$rsedades = mysql_query($query_rsedades, $prueba) or die(mysql_error());
$row_rsedades = mysql_fetch_assoc($rsedades);
$totalRows_rsedades = mysql_num_rows($rsedades);

mysql_select_db($database_prueba, $prueba);
$query_rsproducto = "SELECT * FROM tproducto";
$rsproducto = mysql_query($query_rsproducto, $prueba) or die(mysql_error());
$row_rsproducto = mysql_fetch_assoc($rsproducto);
$totalRows_rsproducto = mysql_num_rows($rsproducto);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin prueba</title>
<body>

<div id="tituloinsrtproducto">INSERTAR nuevo producto</div>
  <form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
    <table align="center">
      <tr valign="baseline">
        <td align="right" nowrap="nowrap" class="txt_formcontacto">Cod_barras:</td>
        <td><input type="text" name="cod_barras" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Nombre</span>:</td>
        <td><input type="text" name="nombre" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Familia</span>:</td>
        <td><select name="familia" id="familia" onChange="llamarAjaxGETsubfamilia()">
            <?php 
do {  
?>
            <option value="<?php echo $row_rs_familia['reffamilia']?>" ><?php echo $row_rs_familia['familia']?></option>
            <?php
} while ($row_rs_familia = mysql_fetch_assoc($rs_familia));
?>
          </select>        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Subfamilia</span>:</td>
        <td><select name="subfamilia" id="subfamilia">
            <?php 
do {  
?>
            <option value="<?php echo $row_rssubfamilia['refsubfamilia']?>" ><?php echo $row_rssubfamilia['subfamilia']?></option>
            <?php
} while ($row_rssubfamilia = mysql_fetch_assoc($rssubfamilia));
?>
          </select>        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Género</span>:</td>
        <td><select name="genero">
            <?php 
do {  
?>
            <option value="<?php echo $row_rsgenero['refgenero']?>" ><?php echo $row_rsgenero['genero']?></option>
            <?php
} while ($row_rsgenero = mysql_fetch_assoc($rsgenero));
?>
          </select>        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Edad</span>:</td>
        <td><select name="edad">
            <?php 
do {  
?>
            <option value="<?php echo $row_rsedades['refedad']?>" ><?php echo $row_rsedades['edad']?></option>
            <?php
} while ($row_rsedades = mysql_fetch_assoc($rsedades));
?>
          </select>        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right" valign="top"><span class="txt_formcontacto">Descripción</span>:</td>
        <td><textarea name="descripcion" cols="50" rows="5"></textarea>        </td>
      </tr>

      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Foto</td>
        <td><label>
          <input type="file" name="foto" id="foto" />
        </label></td>
      </tr>

      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Publicar</span>:</td>
        <td><input type="checkbox" name="publicar" value="" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Novedad</span>:</td>
        <td><input type="checkbox" name="novedad" value="" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Cantidad</span>:</td>
        <td><input type="text" name="cantidad" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"><span class="txt_formcontacto">Precio</span>:</td>
        <td><input type="text" name="precio" value="" size="32" />
          <span class="txt_formcontacto">€</span></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">&nbsp;</td>
        <td><input type="submit" value="Insertar Producto" /></td>
      </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form2" />
  </form>
  <p>&nbsp;</p>
</div> 
<div id="pie">
<div id="azulpie"></div>
</div>

</div>
</body>
</html>
<?php
mysql_free_result($rs_familia);

mysql_free_result($rsgenero);

mysql_free_result($rssubfamilia);

mysql_free_result($rsedades);

mysql_free_result($rsproducto);
procesar.php
Código:
<?php

mysql_connect("localhost","prueba","........");
mysql_select_db("bdprueba");

$rutaEnServidor="foto";
$rutaTemporal=$_FILES['foto']['tmp_name'];
$nombreImagen=$_FILES['foto']['name'];
$rutaDestino=$rutaEnServidor.'/'.$nombreImagen;
move_uploaded_file($rutaTemporal,$rutaDestino);

mysql_query("INSERT INTO tproducto(foto) values('$rutaDestino')");

?>