Buenas, estoy haciendo un formulario que recoge unas variables y permite editar los registros.
He conseguido que cargue las variables, pero al editarlas, me crea un nuevo registro en vez de editarlo. He seguido los consejos de juaniquillo de este post:
http://www.forosdelweb.com/f17/elimi...ro-dw8-475951/
Tengo dos archivos, llistat_continguts.php con la tabla-listado de registrois y el botón de editar y el actualizar.php que es el que uso para cargar los registros y modificarlos.
llistat_continguts.php
Código PHP:
<?php require_once('../Connections/proves_thewilfamily.php'); ?>
<?php
$maxRows_consulta_contingut = 10;
$pageNum_consulta_contingut = 0;
if (isset($_GET['pageNum_consulta_contingut'])) {
$pageNum_consulta_contingut = $_GET['pageNum_consulta_contingut'];
}
$startRow_consulta_contingut = $pageNum_consulta_contingut * $maxRows_consulta_contingut;
mysql_select_db($database_proves_thewilfamily, $proves_thewilfamily);
$query_consulta_contingut = "SELECT * FROM contingut ORDER BY `data` DESC";
$query_limit_consulta_contingut = sprintf("%s LIMIT %d, %d", $query_consulta_contingut, $startRow_consulta_contingut, $maxRows_consulta_contingut);
$consulta_contingut = mysql_query($query_limit_consulta_contingut, $proves_thewilfamily) or die(mysql_error());
$row_consulta_contingut = mysql_fetch_assoc($consulta_contingut);
if (isset($_GET['totalRows_consulta_contingut'])) {
$totalRows_consulta_contingut = $_GET['totalRows_consulta_contingut'];
} else {
$all_consulta_contingut = mysql_query($query_consulta_contingut);
$totalRows_consulta_contingut = mysql_num_rows($all_consulta_contingut);
}
$totalPages_consulta_contingut = ceil($totalRows_consulta_contingut/$maxRows_consulta_contingut)-1;
?><!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=windows-1252" />
<title>llistat_continguts</title>
<link href="../estils.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.Estilo1 {
font-size: large;
font-weight: bold;
}
.Estilo2 {font-size: large}
-->
</style>
</head>
<body>
<table width="70%" border="0">
<tr>
<td width="15"><div align="center"><span class="Estilo1">idcontingut</span></div></td>
<td width="20"><div align="center"><span class="Estilo1">titol</span></div></td>
<td><div align="center"><span class="Estilo1">text</span></div></td>
<td width="15"><div align="center"><span class="Estilo1">categoria</span></div></td>
<td width="15"><div align="center"><span class="Estilo1">data</span></div></td>
<td width="15"><div align="center"><span class="Estilo2"></span></div></td>
<td><div align="center"></div></td>
</tr>
<?php do { ?>
<tr>
<td width="10%"><?php echo $row_consulta_contingut['idcontingut']; ?></td>
<td width="15%"><?php echo $row_consulta_contingut['titol']; ?></td>
<td><?php echo $row_consulta_contingut['text']; ?></td>
<td width="15%"><?php echo $row_consulta_contingut['categoria']; ?></td>
<td width="10%"><?php echo $row_consulta_contingut['data']; ?></td>
<td width="10%"><form action="actualizar.php" method="post" name="form1" class="Estilo2" id="form1">
<label>
<input type="submit" name="Submit" value="Enviar" />
<input name="idcontingut" type="hidden" value="<?php echo $row_consulta_contingut['idcontingut']; ?>" />
</label>
</form> </td>
<td> </td>
</tr>
<?php } while ($row_consulta_contingut = mysql_fetch_assoc($consulta_contingut)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($consulta_contingut);
?>
actualizar.php
Código PHP:
<?php require_once('../Connections/proves_thewilfamily.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 contingut (titol, text, categoria, `data`) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['titol'], "text"),
GetSQLValueString($_POST['text'], "text"),
GetSQLValueString($_POST['categoria'], "text"),
GetSQLValueString($_POST['data'], "date"));
mysql_select_db($database_proves_thewilfamily, $proves_thewilfamily);
$Result1 = mysql_query($insertSQL, $proves_thewilfamily) or die(mysql_error());
$insertGoTo = "correcte.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE contingut SET titol=%s, text=%s, categoria=%s, `data`=%s WHERE idcontingut=%s",
GetSQLValueString($_POST['titol'], "text"),
GetSQLValueString($_POST['text'], "text"),
GetSQLValueString($_POST['categoria'], "text"),
GetSQLValueString($_POST['data'], "date"),
GetSQLValueString($_POST['hiddenField'], "int"));
mysql_select_db($database_proves_thewilfamily, $proves_thewilfamily);
$Result1 = mysql_query($updateSQL, $proves_thewilfamily) or die(mysql_error());
$updateGoTo = "correcte.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
mysql_select_db($database_proves_thewilfamily, $proves_thewilfamily);
$query_consulta_contingut = "SELECT * FROM contingut";
$consulta_contingut = mysql_query($query_consulta_contingut, $proves_thewilfamily) or die(mysql_error());
$row_consulta_contingut = mysql_fetch_assoc($consulta_contingut);
$totalRows_consulta_contingut = mysql_num_rows($consulta_contingut);
$colname_modifica_contingut = "-1";
if (isset($_POST['idcontingut'])) {
$colname_modifica_contingut = (get_magic_quotes_gpc()) ? $_POST['idcontingut'] : addslashes($_POST['idcontingut']);
}
mysql_select_db($database_proves_thewilfamily, $proves_thewilfamily);
$query_modifica_contingut = sprintf("SELECT * FROM contingut WHERE idcontingut = %s", $colname_modifica_contingut);
$modifica_contingut = mysql_query($query_modifica_contingut, $proves_thewilfamily) or die(mysql_error());
$row_modifica_contingut = mysql_fetch_assoc($modifica_contingut);
$totalRows_modifica_contingut = mysql_num_rows($modifica_contingut);
?><!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=windows-1252" />
<title>actualizar</title>
</head>
<body>
<form method="POST" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input name="hiddenField" type="hidden" value="<?php echo $row_consulta_contingut['idcontingut']; ?>" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Titol:</td>
<td><input type="text" name="titol" value="<?php echo $row_modifica_contingut['titol']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Text:</td>
<td><input type="text" name="text" value="<?php echo $row_modifica_contingut['text']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Categoria:</td>
<td><input type="text" name="categoria" value="<?php echo $row_modifica_contingut['categoria']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Data:</td>
<td><input type="text" name="data" value="<?php echo $row_modifica_contingut['data']; ?>" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insertar registro"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
<input type="hidden" name="MM_update" value="form1">
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($consulta_contingut);
mysql_free_result($modifica_contingut);
?>
Saludos!!