Foros del Web » Programando para Internet » PHP »

actualizar fecha en formulario (PHP-MySQL)

Estas en el tema de actualizar fecha en formulario (PHP-MySQL) en el foro de PHP en Foros del Web. Hola a todos, Estoy teniendo un problemita con el formato de fechas que tengo en un formulario.... les cuento: tengo en la BD una tabla ...
  #1 (permalink)  
Antiguo 06/04/2006, 03:53
Avatar de chiquirf  
Fecha de Ingreso: noviembre-2005
Ubicación: Madrid
Mensajes: 215
Antigüedad: 18 años, 5 meses
Puntos: 3
actualizar fecha en formulario (PHP-MySQL)

Hola a todos,

Estoy teniendo un problemita con el formato de fechas que tengo en un formulario.... les cuento: tengo en la BD una tabla con los campos ID, Nombre y Fecha. Tengo un formulario para actualizar los campos Nombre y Fecha, no tengo problema en mostrar las fechas y para agregar una nueva fecha a la tabla, ya q he encontrado un par de funciones para ello:

Código:
// FECHA............. INI
$hoy=strftime("%d/%m/%Y");
function cambiaf_a_mysql($hoy){ 
    ereg( "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})", $hoy, $mifecha); 
    $hoymysql=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1]; 
    return $hoymysql; 
}

function cambiaf_a_normal($fecha){ 
    ereg( "([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})", $fecha, $mifecha); 
    $lafecha=$mifecha[3]."/".$mifecha[2]."/".$mifecha[1]; 
    return $lafecha; 
} 

// FECHA............. FIN

El problema que tengo es que en el formulario para EDITAR, en el IMPUT donde muestro la fecha, ahí mismo quiero cambiarla para enviar el formulario y se guarden los cambios, pero deseo mantener el formato dd/mm/aaaa y no el aaaa-mm-dd del MySQL.

Es decir, cuando entro al formulario me muestra en el IMPUT la fecha con la funcion cambiaf_a_mysql() , de ahi no se que hacer para q al modificarla me la envíe usando la funcion cambiaf_a_mysql() para volver a convertirla a formato SQL y se grabe correctamente.

Alguien por ahi le ha pasado lo mismo y encontró la solución??????

Adjunto los códigos de los .php y una copia de la bd para que lo prueben...

Gracias a todos por su ayuda....

Saludos

Carlos

================================================

ARCHIVO prueba-lista.php
Código PHP:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_Procedimientos "localhost";
$database_Procedimientos "pros_soporte";
$username_Procedimientos "carlos";
$password_Procedimientos "carlos";
$Procedimientos mysql_pconnect($hostname_Procedimientos$username_Procedimientos$password_Procedimientos) or trigger_error(mysql_error(),E_USER_ERROR); 
?>





<?php
mysql_select_db
($database_Procedimientos$Procedimientos);
$query_prueba "SELECT * FROM pruebas ORDER BY ID ASC";
$prueba mysql_query($query_prueba$Procedimientos) or die(mysql_error());
$row_prueba mysql_fetch_assoc($prueba);
$totalRows_prueba mysql_num_rows($prueba);


// FECHA............. INI
$hoy=strftime("%d/%m/%Y");
function 
cambiaf_a_mysql($hoy){ 
    
ereg"([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})"$hoy$mifecha); 
    
$hoymysql=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1]; 
    return 
$hoymysql
}

function 
cambiaf_a_normal($fecha){ 
    
ereg"([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})"$fecha$mifecha); 
    
$lafecha=$mifecha[3]."/".$mifecha[2]."/".$mifecha[1]; 
    return 
$lafecha


// FECHA............. FIN




?>

<html>
<head>
</head>

<body>
Tabla<br />
<br />
<table border="1">
  <tr>
    <td colspan="4">Agregar</td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Nombre</td>
    <td>Fecha</td>
    <td>Editar</td>
  </tr>
  <?php do { ?>
  <tr>
    <td><?php echo $row_prueba['ID']; ?></td>
    <td><?php echo $row_prueba['nombre']; ?></td>
    <td><?php echo cambiaf_a_normal($row_prueba['fecha']); ?></td>
    <td><a href="prueba-edit.php?pruebaid=<?php echo $row_prueba['ID']; ?>">Editar</a></td>
  </tr>
  <?php } while ($row_prueba mysql_fetch_assoc($prueba)); ?>
</table>
</body>
</html>
<?php
mysql_free_result
($prueba);
?>
Los demas los pongo en el siguiente mensaje porq no me permite meter mas.....
  #2 (permalink)  
Antiguo 06/04/2006, 03:55
Avatar de chiquirf  
Fecha de Ingreso: noviembre-2005
Ubicación: Madrid
Mensajes: 215
Antigüedad: 18 años, 5 meses
Puntos: 3
..... continua



ARCHIVO prueba-lista.php
Código PHP:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_Procedimientos "localhost";
$database_Procedimientos "pros_soporte";
$username_Procedimientos "carlos";
$password_Procedimientos "carlos";
$Procedimientos mysql_pconnect($hostname_Procedimientos$username_Procedimientos$password_Procedimientos) or trigger_error(mysql_error(),E_USER_ERROR); 
?>



<?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_update"])) && ($_POST["MM_update"] == "form1")) {
  
$updateSQL sprintf("UPDATE pruebas SET nombre=%s, fecha=%s WHERE ID=%s",
                       
GetSQLValueString($_POST['nombre'], "text"),
                       
GetSQLValueString($_POST['fecha'], "date"),
                       
GetSQLValueString($_POST['ID'], "int"));

  
mysql_select_db($database_Procedimientos$Procedimientos);
  
$Result1 mysql_query($updateSQL$Procedimientos) or die(mysql_error());

  
$updateGoTo "prueba-lista.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$updateGoTo .= (strpos($updateGoTo'?')) ? "&" "?";
    
$updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$updateGoTo));
}


$idprueba $_GET['pruebaid'];
mysql_select_db($database_Procedimientos$Procedimientos);
$query_pruebas "SELECT * FROM pruebas WHERE ID='".$idprueba."'";
$pruebas mysql_query($query_pruebas$Procedimientos) or die(mysql_error());
$row_pruebas mysql_fetch_assoc($pruebas);
$totalRows_pruebas mysql_num_rows($pruebas);


// FECHA............. INI
$hoy=strftime("%d/%m/%Y");
function 
cambiaf_a_mysql($hoy){ 
    
ereg"([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})"$hoy$mifecha); 
    
$hoymysql=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1]; 
    return 
$hoymysql
}

function 
cambiaf_a_normal($fecha){ 
    
ereg"([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})"$fecha$mifecha); 
    
$lafecha=$mifecha[3]."/".$mifecha[2]."/".$mifecha[1]; 
    return 
$lafecha


// FECHA............. FIN

?>

<html >
<head>

</head>

<body>
Editar<br />
<br />
<form method="post" name="form1" action="<?php echo $editFormAction?>">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right">ID:</td>
      <td><?php echo $row_pruebas['ID']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Nombre:</td>
      <td><input type="text" name="nombre" value="<?php echo $row_pruebas['nombre']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Fecha:</td>
      <td><input type="text" name="fecha" value="<?php echo cambiaf_a_normal($row_pruebas['fecha']); ?>" size="32"> dd/mm/aaaa</td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Actualizar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="ID" value="<?php echo $row_pruebas['ID']; ?>">
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result
($pruebas);
?>

..... falta la base de datos

continua.......
  #3 (permalink)  
Antiguo 06/04/2006, 03:56
Avatar de chiquirf  
Fecha de Ingreso: noviembre-2005
Ubicación: Madrid
Mensajes: 215
Antigüedad: 18 años, 5 meses
Puntos: 3
..... continua






Base de datos MySQL - prueba.sql

Código:
-- phpMyAdmin SQL Dump
-- version 2.6.1
-- http://www.phpmyadmin.net
-- 
-- Servidor: localhost
-- Tiempo de generación: 06-04-2006 a las 11:24:06
-- Versión del servidor: 4.1.9
-- Versión de PHP: 4.3.10
-- 
-- Base de datos: `pros_soporte`
-- 

-- --------------------------------------------------------

-- 
-- Estructura de tabla para la tabla `pruebas`
-- 

CREATE TABLE `pruebas` (
  `ID` int(11) NOT NULL default '0',
  `nombre` text collate utf8_spanish_ci NOT NULL,
  `fecha` date NOT NULL default '0000-00-00',
  KEY `ID_PROCEDIMIENTOS` (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;

-- 
-- Volcar la base de datos para la tabla `pruebas`
-- 

INSERT INTO `pruebas` VALUES (1, 'Id=1', '2006-04-06');
INSERT INTO `pruebas` VALUES (2, 'Id=2', '0000-00-00');
INSERT INTO `pruebas` VALUES (3, 'id =3', '0000-00-00');
INSERT INTO `pruebas` VALUES (4, 'id=4', '0000-00-00');
INSERT INTO `pruebas` VALUES (5, 'id=5', '0000-00-00');

---FIN---
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:02.