Ver Mensaje Individual
  #13 (permalink)  
Antiguo 09/10/2008, 08:57
adibu
 
Fecha de Ingreso: septiembre-2007
Mensajes: 50
Antigüedad: 16 años, 7 meses
Puntos: 0
Respuesta: Problemas con la fecha

Estimados compañeros.

Voy consiguiendo cosas pero seguro que hay errores en el código y no acaba de funcionar.

Con el código que facilitó JARONU:
$query = 'INSERT INTO tabla ( fecha)
VALUES (\''.date("Y-m-d").'\')';
mysql_query($query) or die(mysql_error());
echo 'fecha insertada!';

He conseguido que al enviar el formulario se inserte la fecha de envío en el campo Fecha de la tabla general, pero al hacer un envío del formulario, en la tabla se me hacen dos ingresos:
- Uno que contiene los datos incluidos en el formulario con fecha 0000-00-00
- Otro en el que sólo figura la fecha de envío 2008-10-09 y los demás campos de la tabla están vacíos.

Igual he situado mal el código o hay algún otro error, pero sólo me ha ocurrido desde que inserté el código. Para ello os envío el código del formulario y ya me comentáis algo:

<?php require_once('Connections/conexionreformas.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 general (`Nombre_ y _Apellidos`, Dirección, Teléfono, Email, `Plazo_ de_ inicio`, Localidad, Provincia, `Código_ postal`, Descripción, reforma) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['Nombre_y_Apellidos'], "text"),
GetSQLValueString($_POST['Direccin'], "text"),
GetSQLValueString($_POST['Telfono'], "text"),
GetSQLValueString($_POST['Email'], "text"),
GetSQLValueString($_POST['Plazo_de_inicio'], "text"),
GetSQLValueString($_POST['Localidad'], "text"),
GetSQLValueString($_POST['Provincia'], "text"),
GetSQLValueString($_POST['Cdigo_postal'], "text"),
GetSQLValueString($_POST['Descripcin'], "text"),
GetSQLValueString($_POST['obra_realizar'], "text"));

mysql_select_db($database_conexionreformas, $conexionreformas);
$Result1 = mysql_query($insertSQL, $conexionreformas) or die(mysql_error()); $query = 'INSERT INTO general ( fecha)
VALUES (\''.date("Y-m-d").'\')';
mysql_query($query) or die(mysql_error());
echo 'fecha insertada!';
$insertGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

?><!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>Documento sin t&iacute;tulo</title>
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td width="232" align="right" nowrap>Nombre y Apellidos:</td>
<td> <div align="center">
<input type="text" name="Nombre_y_Apellidos" value="" size="40" />
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Dirección:</td>
<td> <div align="center">
<input type="text" name="Direccin" value="" size="40">
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Teléfono:</td>
<td> <div align="center">
<input type="text" name="Telfono" value="" size="40">
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Email:</td>
<td> <div align="center">
<input type="text" name="Email" value="" size="40">
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Inicio de la obra:</td>
<td><div align="center">
<input type="text" name="Plazo_de_inicio" value="" size="40" />
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Localidad de la obra:</td>
<td> <div align="center">
<input type="text" name="Localidad" value="" size="40">
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Provincia de la obra:</td>
<td><div align="center">
<input type="text" name="Provincia" value="" size="40" />
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Código postal:</td>
<td><div align="center">
<input type="text" name="Cdigo_postal" value="" size="40" />
</div></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Obra a realizar:<br />
(Ej: Reformar piso) </td>
<td><div align="center">
<input name="obra_realizar" type="text" id="obra_realizar" value="" size="40" />
</div></td>
</tr>
<tr valign="baseline">
<td align="right" valign="middle" nowrap>Descripci&oacute;n de la obra: </td>
<td><label>
<div align="center">
<textarea name="Descripcin" cols="34" rows="5" id="Descripcin"></textarea>
</div>
</label></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input name="submit" type="submit" value="Enviar" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>