PHP - Subir imagen en php y añadir ruta a MySQL

 
Vista:

Subir imagen en php y añadir ruta a MySQL

Publicado por Nelson (1 intervención) el 03/11/2011 20:55:27
Pues eso... No tengo mucha idea de PHP y necesitaba modificar una página que tengo para subir datos a MySQL e incorporar la posibilidad de subir una imagen a un directorio del servidor y que almacene la ruta en la base de datos.

Si me pudieseis ayudar os lo agradecería!!

La página con el formulario y el scrip para subir los datos es el siguiente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php require_once('Connections/conexion_libros.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];
 
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 libros (libro_id, nombre_libro, descripcion, precio) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['libro_id'], "text"),
                       GetSQLValueString($_POST['nombre_libro'], "text"),
                       GetSQLValueString($_POST['descripcion'], "text"),
                       //GetSQLValueString($_POST['Cantidad'], "int"),
					   GetSQLValueString($_POST['precio'], "double")
					   );
 
  mysql_select_db($database_conexion_libros, $conexion_libros);
  $Result1 = mysql_query($insertSQL, $conexion_libros) or die(mysql_error());
 
  $insertGoTo = "ingreso_exitoso.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
 
 
$maxRows_consulta_libros = 5;
$pageNum_consulta_libros = 0;
if (isset($_GET['pageNum_consulta_libros'])) {
  $pageNum_consulta_libros = $_GET['pageNum_consulta_libros'];
}
$startRow_consulta_libros = $pageNum_consulta_libros * $maxRows_consulta_libros;
 
mysql_select_db($database_conexion_libros, $conexion_libros);
$query_consulta_libros = "SELECT * FROM libros";
$query_limit_consulta_libros = sprintf("%s LIMIT %d, %d", $query_consulta_libros, $startRow_consulta_libros, $maxRows_consulta_libros);
$consulta_libros = mysql_query($query_limit_consulta_libros, $conexion_libros) or die(mysql_error());
$row_consulta_libros = mysql_fetch_assoc($consulta_libros);
 
if (isset($_GET['totalRows_consulta_libros'])) {
  $totalRows_consulta_libros = $_GET['totalRows_consulta_libros'];
} else {
  $all_consulta_libros = mysql_query($query_consulta_libros);
  $totalRows_consulta_libros = mysql_num_rows($all_consulta_libros);
}
$totalPages_consulta_libros = ceil($totalRows_consulta_libros/$maxRows_consulta_libros)-1;
 
$queryString_consulta_libros = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_consulta_libros") == false &&
        stristr($param, "totalRows_consulta_libros") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_consulta_libros = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_consulta_libros = sprintf("&totalRows_consulta_libros=%d%s", $totalRows_consulta_libros, $queryString_consulta_libros);
?><!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=iso-8859-1" />
<title>Prueba Base Libros</title>
<style type="text/css">
<!--
body {
	background-color: #999999;
}
-->
</style></head>
 
<body>
<table width="80%" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC" bgcolor="#FFFFFF">
  <tr>
    <td><div align="center"><strong>C&oacute;digo</strong></div></td>
    <td><div align="center"><strong>T&iacute;tulo</strong></div></td>
    <td><div align="center"><strong>Descripci&oacute;n</strong></div></td>
    <td><div align="center"><strong>Cantidad</strong></div></td>
    <td><div align="center"><strong>Precio</strong></div></td>
    <td colspan="2"><div align="center"><strong>Operaciones</strong></div></td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_consulta_libros['libro_id']; ?></td>
      <td><div align="center"><?php echo $row_consulta_libros['nombre_libro']; ?></div></td>
      <td><?php echo $row_consulta_libros['descripcion']; ?></td>
      <td><div align="center"><?php echo $row_consulta_libros['Cantidad']; ?></div></td>
      <td><div align="center"><?php echo $row_consulta_libros['precio']; ?></div></td>
      <td><a href="modificar.php?libro_id=<?php echo $row_consulta_libros['libro_id']; ?>">Modificar</a></td>
      <td><a href="borrar.php?libro_id=<?php echo $row_consulta_libros['libro_id']; ?>">Eliminar</a></td>
    </tr>
    <?php } while ($row_consulta_libros = mysql_fetch_assoc($consulta_libros)); ?>
</table>
<p>
<table border="0" width="50%" align="center">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_consulta_libros > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_consulta_libros=%d%s", $currentPage, 0, $queryString_consulta_libros); ?>">Primero</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_consulta_libros > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_consulta_libros=%d%s", $currentPage, max(0, $pageNum_consulta_libros - 1), $queryString_consulta_libros); ?>">Anterior</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_consulta_libros < $totalPages_consulta_libros) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_consulta_libros=%d%s", $currentPage, min($totalPages_consulta_libros, $pageNum_consulta_libros + 1), $queryString_consulta_libros); ?>">Siguiente</a>
          <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_consulta_libros < $totalPages_consulta_libros) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_consulta_libros=%d%s", $currentPage, $totalPages_consulta_libros, $queryString_consulta_libros); ?>">&Uacute;ltimo</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
</p>
 
 
 
<form method="post" name="form1" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data">
  <table align="center" bordercolor="#0A4975" bgcolor="#FFFFFF">
    <tr valign="baseline">
      <td nowrap align="right">Id:</td>
      <td><input type="text" name="libro_id" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">T&iacute;tulo:</td>
      <td><input name="nombre_libro" type="text" value="" size="100" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Descripcion:</td>
      <td><textarea name="descripcion" cols="100" rows="10"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Imagen:</td>
      <td><input name="imagen" type="file" size="32" ></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Precio:</td>
      <td><input type="text" name="precio" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insertar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p><a href="libros.php">Ver libros </a></p>
</body>
</html>
<?php
mysql_free_result($consulta_libros);
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder