Ver Mensaje Individual
  #3 (permalink)  
Antiguo 23/10/2012, 07:32
dorado145
 
Fecha de Ingreso: octubre-2012
Ubicación: trujillo
Mensajes: 2
Antigüedad: 11 años, 6 meses
Puntos: 0
Respuesta: editar datos de tablas relacionadas de mysql en php

Bueno gracias maycolalvares por responder este aqui te muestro mi codigo la verdad si tuviera errores me lo podrias decir para poder corregirlo esq en realidad es la primera ves q trabajo con tablas relacionadas en mysql.

funciones.php
Código php:
Ver original
  1. <?php
  2. function getParam($param, $default) {
  3.     $result = $default;
  4.     if (isset($param)) {
  5.         $result = (get_magic_quotes_gpc()) ? $param : addslashes($param);
  6.     }
  7.     return $result;
  8. }
  9. function sqlValue($value, $type) {
  10.   $value = get_magic_quotes_gpc() ? stripslashes($value) : $value;
  11.   $value = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($value) : mysql_escape_string($value);
  12.   switch ($type) {
  13.     case "text":
  14.       $value = ($value != "") ? "'" . $value . "'" : "NULL";
  15.       break;
  16.     case "int":
  17.       $value = ($value != "") ? intval($value) : "NULL";
  18.       break;
  19.     case "double":
  20.       $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";
  21.       break;
  22.     case "date":
  23.       $value = ($value != "") ? "'" . $value . "'" : "NULL";
  24.       break;
  25.   }
  26.   return $value;
  27. }
  28. ?>


listado.php
Código PHP:
Ver original
  1. <body>
  2. <h3>Listado de  Empresas</h3>
  3. <table width="600" border="0" cellspacing="0" cellpadding="0">
  4.   <tr>
  5.     <th>Nombre</th>
  6.     <th>Apellidos</th>
  7.     <th>Telefono</th>
  8.     <th>Correo</th>
  9.     <th>Egreso</th>
  10.     <th>Empresa</th>
  11.     <th>Dirección</th>
  12.     <th>Rubro</th>
  13.     <th>Cargo</th>
  14.     <th>Pais</th>
  15.     <th>Opcion</th>
  16.     <th>&nbsp;</th>
  17.     <th>&nbsp;</th>
  18.   </tr>
  19.   <?php while ($rsEmp = mysql_fetch_assoc($queEmp)) { ?>
  20.   <tr>
  21.     <td><?php echo $rsEmp['nombre']; ?></td>
  22.     <td><?php echo $rsEmp['apellidos']; ?></td>
  23.     <td><?php echo $rsEmp['telefono']; ?></td>
  24.     <td><?php echo $rsEmp['correo']; ?></td>
  25.     <td><?php echo $rsEmp['egreso']; ?></td>
  26.     <td><?php echo $rsEmp['empresa']; ?></td>
  27.     <td><?php echo $rsEmp['direccion']; ?></td>
  28.     <td><?php echo $rsEmp['rubro']; ?></td>
  29.     <td><?php echo $rsEmp['cargo']; ?></td>
  30.     <td><?php echo $rsEmp['pais']; ?></td>
  31.     <td><a href="editar.php?idegresado=<?php echo $rsEmp['idegresado']; ?>">Editar</a></td>
  32.  
  33.   </tr>
  34.   <?php } ?>
  35. </table>
  36.  
  37. <p>&nbsp;</p>
  38. </body>
  39. </html>


editar.php
Código PHP:
Ver original
  1. require("conexion.php");
  2. require("funciones.php");
  3. //leer lista de cargos
  4. $sql = "select idcargo, cargo from cargo ";
  5. $cargos = mysql_query( $sql, $conexion );
  6. //leer lista de paises
  7. $sql = "select idpais, pais from paises ";
  8. $paises = mysql_query( $sql, $conexion );
  9.  
  10.  
  11. $idegresado = getParam($_GET["idegresado"], "-1");
  12. $action = getParam($_GET["action"], "");
  13.  
  14. if ($action == "edit") {
  15.     $idegresado = getParam($_POST["idegresado"], "-1");
  16.     $nombre = sqlValue($_POST["nombre"], "text");
  17.     $apellidos = sqlValue($_POST["apellidos"], "text");
  18.     $telefono = sqlValue($_POST["telefono"], "int");
  19.     $correo = sqlValue($_POST["correo"], "text");
  20.     $egreso = sqlValue($_POST["egreso"], "int");
  21.     $empresa = sqlValue($_POST["empresa"], "text");
  22.     $direccion = sqlValue($_POST["direccion"], "text");
  23.     $rubro = sqlValue($_POST["rubro"], "text");
  24.     $cargo = sqlValue($_POST["cargo"], "text");
  25.     $pais = sqlValue($_POST["pais"], "text");
  26.     $sql = "UPDATE egresado SET
  27.     nombre=".$nombre.", apellidos=".$apellidos.", telefono=".$telefono.", correo=".$correo.", egreso=".$egreso.", empresa=".$empresa.", direccion=".$direccion.", rubro=".$rubro.", cargo=".$cargo.", pais=".$pais."
  28.     WHERE idegresado=".$idegresado;
  29.     mysql_query($sql, $conexion) or die (mysql_error());
  30.     header("location: listado.php") ;
  31. }
  32.  
  33. $sql = "SELECT egresado.idegresado, egresado.nombre, egresado.apellidos, egresado.telefono, egresado.correo, egresado.egreso, empresa.empresa, empresa.direccion, empresa.rubro,cargo.cargo, paises.pais
  34.                 FROM egresado, empresa, cargo, paises
  35.                 WHERE egresado.idempresa = empresa.idempresa
  36.                 AND empresa.idcargo = cargo.idcargo
  37.                 AND empresa.idpais = paises.idpais
  38.                 AND egresado.idegresado = ".sqlValue($idegresado, "int");
  39. $queEmp = mysql_query($sql, $conexion);
  40. $rsEmp = mysql_fetch_assoc($queEmp);
  41. $total = mysql_num_rows($queEmp);
  42. if ($total == 0) {
  43.     header("location: listado.php");
  44.     exit;
  45. }
  46. ?>
  47. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  48. <html xmlns="http://www.w3.org/1999/xhtml">
  49. <head>
  50. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  51. <title>PHP con MySQL: Editar datos en MySQL</title>
  52. <link href="styles.css" rel="stylesheet" type="text/css" />
  53. </head>
  54. <body>
  55. <h3>Editar Empresa</h3>
  56. <?php if ($status == "ok") { ?>
  57. <p class="confirm">Registro guardado correctamente</p>
  58. <?php } ?>
  59. <form method="post" id="frEmpresa" action="editar.php?action=edit">
  60.     <label for="nombre">Nombre</label>
  61.     <input type="text" id="nombre" name="nombre" value="<?php echo $rsEmp["nombre"]; ?>" />
  62.     <br />
  63.     <label for="apellidos">Apellidos</label>
  64.     <input type="text" id="apellidos" name="apellidos" value="<?php echo $rsEmp["apellidos"]; ?>" />
  65.     <br />
  66.     <label for="telefono">Telefono</label>
  67.     <input type="text" id="telefono" name="telefono" value="<?php echo $rsEmp["telefono"]; ?>" />
  68.     <br />
  69.     <label for="correo">Correo</label>
  70.     <input type="text" id="correo" name="correo" value="<?php echo $rsEmp["correo"]; ?>" />
  71.     <br />
  72.     <label for="egreso">Nombre</label>
  73.     <input type="text" id="egreso" name="egreso" value="<?php echo $rsEmp["egreso"]; ?>" />
  74.     <br />
  75.     <label for="empresa">Empresa</label>
  76.     <input type="text" id="empresa" name="empresa" value="<?php echo $rsEmp["empresa"]; ?>" />
  77.     <br />
  78.     <label for="direccion">Direcci&oacute;n</label>
  79.     <input type="text" id="direccion" name="direccion" value="<?php echo $rsEmp["direccion"]; ?>" />
  80.     <br />
  81.     <label for="rubro">Rubro</label>
  82.     <input type="text" id="rubro" name="rubro" value="<?php echo $rsEmp["rubro"]; ?>" />
  83.     <br />
  84.     <label for="cargo">Cargo</label>
  85.     <select name="cargo" id="cargo" size="1">
  86.                             <?php while( $rsEmp = mysql_fetch_assoc( $cargos ) ) { ?>
  87.                             <option value="<?php echo( $rsEmp["idcargo"] ) ?>">
  88.                                 <?php echo( $rsEmp["cargo"] ) ?>
  89.                                 <?php echo "selected" ?>
  90.                             </option>
  91.                             <?php } ?>
  92.                         </select>
  93.     <br />
  94.     <label for="pais">Pais</label>
  95.     <select name="pais" id="pais" size="1">
  96.                             <?php while( $rsEmp = mysql_fetch_assoc( $paises ) ) { ?>
  97.                             <option value="<?php echo( $rsEmp["idpais"] ) ?>">
  98.                                 <?php echo( $rsEmp["pais"] ) ?>
  99.                                 <?php echo 'selected' ?>
  100.                             </option>
  101.                             <?php } ?>
  102.                         </select>
  103.     <br />
  104.     <label for="bts">&nbsp;</label>
  105.     <button type="submit">Guardar</button>
  106.     <button type="reset">Limpiar</button>
  107.     <input type="hidden" id="idegresado" name="idegresado" value="<?php echo $rsEmp["idegresado"]; ?>" />
  108. </form>
  109. </body>
  110. </html>


espero me respondas y me puedas ayudar en lo mas pronto posible gracias de antemano tus respuestas

Última edición por dorado145; 23/10/2012 a las 07:40