Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/10/2011, 05:04
fran1
 
Fecha de Ingreso: octubre-2011
Mensajes: 6
Antigüedad: 12 años, 7 meses
Puntos: 0
Como se pueden insertar registros en una base de datos usando una variable

Hola
Estoy intentando crear un foro y tengo un problema a la hora de insertar algunos registros en la base de datos.
A continuación incluyo el código php y el formulario. El formulario funciona bien e inserta los registros que se piden en los campos de texto, el problema es que además quiero insertar el valor de la fecha actual en el registro 'fecha' de la base de datos, y en un campo llamado 'id_mensaje' debo insertar un valor que relaciona la respuesta con el mensaje al que contesta.
Por mucho que lo he intentado no consigo insertar esos valores que tengo asignados en unas variables llamadas $fecha_actual y $Mensaje_inicial en la base de datos.
¿Alguien puede ayudarme e indicarme como hacerlo?


<?php require_once('Connections/foro.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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"] == "Responder")) {
$insertSQL = sprintf("INSERT INTO respuestas (autor, respuesta) VALUES (%s, %s)",
GetSQLValueString($_POST['Autor'], "text"),
GetSQLValueString($_POST['Respuesta'], "text"),
mysql_select_db($database_foro, $foro));
$Result1 = mysql_query($insertSQL, $foro) or die(mysql_error());


$insertGoTo = "Respuestas.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
Y ESTE ES EL FORMULARIO:
<form action="<?php echo $editFormAction; ?>" id="Responder" name="Responder" method="POST">
<table width="392" height="205" border="1" align="center" cellpadding="4" cellspacing="2">
<tr>
<td height="36">AUTOR:</td>
<td><input name="Autor" type="text" id="Autor" maxlength="30" /></td>
</tr>
<tr>
<td>RESPUESTA:</td>
<td><textarea name="Respuesta" id="Respuesta"></textarea></td>
</tr>
<tr>
<td height="34">&nbsp;</td>
<td align="center" valign="middle"><input type="submit" name="Crear" id="Crear" value="Enviar" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="Responder" />
</form>