Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Duda - Insertar Fecha y Hora del Sistema

Estas en el tema de Duda - Insertar Fecha y Hora del Sistema en el foro de PHP en Foros del Web. Buenos días, tengo realizado un formulario de Update y necesito que al tildar el checkbox "fecha_inicio" se inserte en la base la fecha y hora ...
  #1 (permalink)  
Antiguo 17/07/2014, 08:02
 
Fecha de Ingreso: julio-2008
Mensajes: 57
Antigüedad: 15 años, 9 meses
Puntos: 0
Duda - Insertar Fecha y Hora del Sistema

Buenos días, tengo realizado un formulario de Update y necesito que al tildar el checkbox "fecha_inicio" se inserte en la base la fecha y hora del sistema y al tildar el checkbox "fecha_fin" también.

Le paso el fragmento de código del formulario de Update:

Código PHP:
Ver original
  1. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  2.   <input name="fecha_inicio" id="fecha_inicio" type="checkbox" value="Inicio" />
  3.   <input name="fecha_fin" id="fecha_fin" type="checkbox" value="Inicio" />
  4.   <input type="image" src="../assets/img/btn_guardar_gestionar.jpg" />
  5.   <input type="hidden" name="MM_update" value="form1" />
  6.   <input type="hidden" name="id" value="<?php echo $row_rs_detalle['id']; ?>" />
  7. </form>

El fragmento de código del Update:

Código PHP:
Ver original
  1. if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  2.   $updateSQL = sprintf("UPDATE presupuestos SET fecha_inicio=%s, fecha_fin=%s WHERE id=%s",
  3.                        GetSQLValueString($_POST['fecha_inicio'], "text"),
  4.                        GetSQLValueString($_POST['fecha_fin'], "text"),
  5.                        GetSQLValueString($_POST['id'], "int"));

Y el código completo por las dudas:

Código PHP:
Ver original
  1. <?php require_once('../assets/Connections/cnx_m.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38.  
  39. if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  40.   $updateSQL = sprintf("UPDATE presupuestos SET fecha_inicio=%s, fecha_fin=%s WHERE id=%s",
  41.                        GetSQLValueString($_POST['fecha_inicio'], "text"),
  42.                        GetSQLValueString($_POST['fecha_fin'], "text"),
  43.                        GetSQLValueString($_POST['id'], "int"));
  44.  
  45.   mysql_select_db($database_cnx_m, $cnx_m);
  46.   $Result1 = mysql_query($updateSQL, $cnx_m) or die(mysql_error());
  47.  
  48.   $updateGoTo = "ok.php";
  49.   if (isset($_SERVER['QUERY_STRING'])) {
  50.     $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
  51.     $updateGoTo .= $_SERVER['QUERY_STRING'];
  52.   }
  53.   header(sprintf("Location: %s", $updateGoTo));
  54. }
  55.  
  56. $colname_rs_detalle = "-1";
  57. if (isset($_GET['id'])) {
  58.   $colname_rs_detalle = $_GET['id'];
  59. }
  60. mysql_select_db($database_cnx_m, $cnx_m);
  61. $query_rs_detalle = sprintf("SELECT * FROM presupuestos WHERE id = %s", GetSQLValueString($colname_rs_detalle, "int"));
  62. $rs_detalle = mysql_query($query_rs_detalle, $cnx_m) or die(mysql_error());
  63. $row_rs_detalle = mysql_fetch_assoc($rs_detalle);
  64. $totalRows_rs_detalle = mysql_num_rows($rs_detalle);
  65. ?>
  66. <html>
  67. <head>
  68. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  69. <title></title>
  70. </head>
  71. <body>
  72. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  73.   <input name="fecha_inicio" id="fecha_inicio" type="checkbox" value="Inicio" />
  74.   <input name="fecha_fin" id="fecha_fin" type="checkbox" value="Inicio" />
  75.   <input type="image" src="../assets/img/btn_guardar_gestionar.jpg" />
  76.   <input type="hidden" name="MM_update" value="form1" />
  77.   <input type="hidden" name="id" value="<?php echo $row_rs_detalle['id']; ?>" />
  78. </form>
  79. </body>
  80. </html>
  81. <?php
  82. mysql_free_result($rs_detalle);
  83. ?>

Me podrán orientar como debo plantearlo?

Muchas gracias!
  #2 (permalink)  
Antiguo 17/07/2014, 11:06
Avatar de cluster28  
Fecha de Ingreso: enero-2008
Ubicación: Donostia - San Sebastián
Mensajes: 756
Antigüedad: 16 años, 3 meses
Puntos: 32
Respuesta: Duda - Insertar Fecha y Hora del Sistema

Si no he entendido mal, con comprobar si $_POST['fecha_inicio'] y $_POST['fecha_fin'] están tildados y en el UPDATE poner la hora en el formato que tengas es suficiente.

Una solución podría ser (es muy guarro y no recomendable pero es para orientarte):

Código PHP:

if(isset($_POST['fecha_inicio'])){
      
$_POST['fecha_inicio'] = date('d-m-y');
}

if(isset(
$_POST['fecha_fin'])){
      
$_POST['fecha_fin'] = date('d-m-y');
}


//Y aquí tu código. 
Un checkbox no envía datos...
  #3 (permalink)  
Antiguo 17/07/2014, 12:05
 
Fecha de Ingreso: julio-2008
Mensajes: 57
Antigüedad: 15 años, 9 meses
Puntos: 0
Respuesta: Duda - Insertar Fecha y Hora del Sistema

Ok, ya que el chekbox no evía datos veré de hacerlo con un inputbox y un datepicker con hora.

Gracias!

Etiquetas: fecha, formulario, html, mysql, select, sistema, sql
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 18:46.