Ver Mensaje Individual
  #4 (permalink)  
Antiguo 15/01/2008, 10:15
Okiro
 
Fecha de Ingreso: mayo-2005
Mensajes: 51
Antigüedad: 19 años
Puntos: 0
Re: Fotos en una Base de datos

Mira, esto es lo que he estado haciendo, pero no funciona. Algo hago mal:

Codigo del formulario:
Código PHP:
 <?php require_once('Connections/pcsTHC.php'); ?>
<?php
function GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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"] == "form1")) {
  
$insertSQL sprintf("INSERT INTO modelo (id_modelo, modelo) VALUES (%s, %s)",
                       
GetSQLValueString($_POST['id_modelo'], "int"),
                       
GetSQLValueString($_POST['modelo'], "text"));

  
mysql_select_db($database_pcsTHC$pcsTHC);
  
$Result1 mysql_query($insertSQL$pcsTHC) or die(mysql_error());
}

mysql_select_db($database_pcsTHC$pcsTHC);
$query_Recordset1 "SELECT * FROM fotos";
$Recordset1 mysql_query($query_Recordset1$pcsTHC) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);

mysql_select_db($database_pcsTHC$pcsTHC);
$query_Recordset2 "SELECT * FROM modelo";
$Recordset2 mysql_query($query_Recordset2$pcsTHC) or die(mysql_error());
$row_Recordset2 mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 mysql_num_rows($Recordset2);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>{::::... -- THC Tecnolog&iacute;a del Hardware Cl&oacute;nico --...::::}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
    margin-left: 0px;
    margin-top: 5px;
    background-image: url(IMAGENES-WEB/fondo-azul.png);
}
-->
</style></head>

<body>
<table width="671" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#FFFFFF"><div align="center">
    </div>      <table width="730" border="0">
        <tr>
          <td valign="top">&nbsp;
<form method="post" name="form1" action="<?php echo $editFormAction?>">
  <table align="center">
                <tr valign="baseline">
                  <td nowrap align="right">Id_modelo:</td>
                  <td><input type="text" name="id_modelo" value="" size="32"></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">Modelo:</td>
                  <td><input type="text" name="modelo" value="" size="32"></td>
                </tr>
                <tr>
                  <td><a href="javascript:void(0)" onclick="open('subir.php?e=0','newwin','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,width=420,height=150')"><font face="Verdana" color="#37699A"><span style="font-size:9pt;"><b>SUBIR</b></span></font></a><font face="Verdana" color="#37699A"><span style="font-size:9pt;">&nbsp;</span></font></td>
                  <td><font face="Verdana" color="#37699A"><span style="font-size:9pt;"><b>
                    <input name="foto" type="text" id="foto" style="background-image:url('../form.jpg'); border-width:1; border-color:rgb(134,171,187); border-style:solid;" value="http://" size="80">
                  </b></span></font></td>
                </tr>
                <tr valign="baseline">
                  <td nowrap align="right">&nbsp;</td>
                  <td><input name="submit" type="submit" value="Insertar registro"></td>
                </tr>
              </table>
              <input type="hidden" name="MM_insert" value="form1">
            </form>
          <p>&nbsp;</p></td>
        </tr>
      </table>    </td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result
($Recordset1);

mysql_free_result($Recordset2);
?>



codigo del archivo que subir.php

Código PHP:
<form method="post" action="guardar.php" enctype="multipart/form-data">
Nombre: <input type="text" name="nombre de foto " maxlength="100"><br>
Imagen: <input type="file" name="foto" size="40"><br>
<
p><input type="submit" name="submit" value="Subir">
</
form

codigo archivo guardar.php

Código PHP:
<?php require_once('config.php'); ?> <?php 
$directori_foto 
"\fotos"//pon aqui el directorio donde se subiran las fotos, sino sabes como es mira creando un archivo phpinfo.php para que te ponga la ruta, suele ser algo tipo: /home/usuario/public_html/
 
$nombre strtr($_FILES['foto']['name'], "ñ'ÁÉÍÓÚÀÈÌÒÙáéíóúäëïöüàèìòù ""n-AEIOUAEIOUaeiouaeiouaeiou_"); 
$tamanio $_FILES['foto']['size'];
$tipo $_FILES['foto']['type'];
global 
$HTTP_POST_VARS
$ar "http://www.-------.com/fotos/".$nombre//aqui la url donde se sube la foto
copy($_FILES['foto']['tmp_name'],$directori_foto.$nombre);
echo 
"<font color=cc0000 face=verdana size=1>el archivo se a subido correctamente al servidor<br>"
 
$db = @mysql_pconnect($host,$user,$pass); //o como conectes tu con tu base de datos
 
if( !$db ){
echo 
"Error: no se a podido conectar a la base de datos";
exit;
}
mysql_select_db($dbname,$db); //selecciona la tuya 
$sql "INSERT INTO fotos (id, foto) VALUES ('','$ar')";
$result mysql_query$sql$db) ; 
if( 
$result ) {
echo 
"datos añadidos a la base de datos con exito"
}
else {
echo 
"Error: no se a podido añadir a la base de datos.";
}
echo 
"<br><br>";
?> 
<script language="Javascript">
<!--
function insert(url) {
opener.<? echo $form?>.>foto.value = url;
window.close();
}
//-->
</script>
</head>
<body>
<center>
</center>
<br><br>
<? echo "$url\n"?><br>
<br>
<center><a href="javascript:insert('<? echo $url?>')">Insertar URL directamente</a></center>
</body>
</html>


se supone que este ultimo archivo lo que haría sería insertar en el campo de texto "foto" la url del archivo que acaba de subir al servidor.