Foros del Web » Programando para Internet » PHP »

error al actualizar los datos

Estas en el tema de error al actualizar los datos en el foro de PHP en Foros del Web. lo amigos espero me puedan ayudar una vez mas, tengo el siguiente codigo y me marcha bien pero solo me deja actualizar el ultimo registro ...
  #1 (permalink)  
Antiguo 12/06/2011, 15:13
Usuario no validado
 
Fecha de Ingreso: enero-2011
Mensajes: 36
Antigüedad: 13 años, 3 meses
Puntos: 0
Exclamación error al actualizar los datos

lo amigos espero me puedan ayudar una vez mas, tengo el siguiente codigo y me marcha bien pero solo me deja actualizar el ultimo registro ingresado los primeros no los puedo alterar no se que me faltara, aqui les dejo el codigo.

<?php require_once('Connections/informacion.php'); ?>
<?php require_once('Connections/informacion.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$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;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$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_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE personal SET Nombre_de_la_Pagina=%s, Descripcion=%s, Fecha=%s, `Direccion_Url`=%s, Hipervinculo=%s, Usuario=%s, Password=%s WHERE id=%s",
GetSQLValueString($_POST['Nombre_de_la_Pagina'], "text"),
GetSQLValueString($_POST['Descripcion'], "text"),
GetSQLValueString($_POST['Fecha'], "date"),
GetSQLValueString($_POST['Direccion_Url'], "text"),
GetSQLValueString($_POST['Hipervinculo'], "text"),
GetSQLValueString($_POST['Usuario'], "text"),
GetSQLValueString($_POST['Password'], "int"),
GetSQLValueString($_POST['id'], "int"));

mysql_select_db($database_informacion, $informacion);
$Result1 = mysql_query($updateSQL, $informacion) or die(mysql_error());

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

mysql_select_db($database_informacion, $informacion);
$query_Recordset = "SELECT * FROM personal";
$Recordset = mysql_query($query_Recordset, $informacion) or die(mysql_error());
$row_Recordset = mysql_fetch_assoc($Recordset);
$totalRows_Recordset = mysql_num_rows($Recordset);
?><p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center">
<label></label>
</p>
<form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<label></label>
<label></label>
<label></label>
<label></label>
<label></label>
<label></label>
<label></label>
<label></label>

<?php do { ?>
<table border="1">
<tr>
<td><input name="id" type="text" id="id" value="<?php echo $row_Recordset['id']; ?>" size="5"></td>
<td><input name="Nombre_de_la_Pagina" type="text" id="Nombre_de_la_Pagina" value="<?php echo $row_Recordset['Nombre_de_la_Pagina']; ?>"></td>
<td><input name="Descripcion" type="text" id="Descripcion" value="<?php echo $row_Recordset['Descripcion']; ?>"></td>
<td><input name="Fecha" type="text" id="Fecha" value="<?php echo $row_Recordset['Fecha']; ?>"></td>
<td><input name="Direccion_Url" type="text" id="Direccion_Url" value="<?php echo $row_Recordset['Direccion_Url']; ?>"></td>
<td><input name="Hipervinculo" type="text" id="Hipervinculo" value="<?php echo $row_Recordset['Hipervinculo']; ?>"></td>
<td><input name="Usuario" type="text" id="Usuario" value="<?php echo $row_Recordset['Usuario']; ?>"></td>
<td><input name="Password" type="text" id="Password" value="<?php echo $row_Recordset['Password']; ?>"></td>
</tr>
</table>
<?php } while ($row_Recordset = mysql_fetch_assoc($Recordset)); ?>
<p>&nbsp;</p>
<p align="center">
<label>
<input type="submit" name="Actualizar" id="Actualizar" value="Actualizar">
</label>
<label>
<input type="reset" name="Restablecer" id="Restablecer" value="Restablecer">
</label>
</p>
<p>&nbsp;</p>
<input type="hidden" name="MM_update" value="form1">
</form>
<p align="center">&nbsp;</p>
<p>
<?php
mysql_free_result($Recordset);
?>
  #2 (permalink)  
Antiguo 12/06/2011, 17:33
Avatar de the_web_saint  
Fecha de Ingreso: mayo-2008
Ubicación: localhost/tierra/america/panama
Mensajes: 1.229
Antigüedad: 15 años, 11 meses
Puntos: 43
Respuesta: error al actualizar los datos

Hola,

Intentaste hacer un echo a lo que tiene $updateSQL. ?

A simple vista no veo problemas con el que actualice solo el último registro.

Slds
__________________
..::The Saint::..
El pesimista se queja del viento; el optimista espera que cambie; el realista ajusta las velas.
  #3 (permalink)  
Antiguo 12/06/2011, 19:46
Usuario no validado
 
Fecha de Ingreso: enero-2011
Mensajes: 36
Antigüedad: 13 años, 3 meses
Puntos: 0
Exclamación Respuesta: error al actualizar los datos

no simplemente que probe y los primeros registros no los actualiza solo el ultimo, e revisado el codigo mil veces y no doy con el error!!!! necesito ayuda urgente
  #4 (permalink)  
Antiguo 12/06/2011, 22:50
Avatar de gildus  
Fecha de Ingreso: agosto-2003
Mensajes: 1.495
Antigüedad: 20 años, 8 meses
Puntos: 105
Respuesta: error al actualizar los datos

Holas,

A simple vista, veo que estas haciendo un bucle:

Código PHP:
Ver original
  1. <?php do { ?>
  2. <table border="1">
  3. <tr>
  4. <td><input name="id" type="text" id="id" value="<?php echo $row_Recordset['id']; ?>" size="5"></td>
  5. <td><input name="Nombre_de_la_Pagina" type="text" id="Nombre_de_la_Pagina" value="<?php echo $row_Recordset['Nombre_de_la_Pagina']; ?>"></td>
  6. <td><input name="Descripcion" type="text" id="Descripcion" value="<?php echo $row_Recordset['Descripcion']; ?>"></td>
  7. <td><input name="Fecha" type="text" id="Fecha" value="<?php echo $row_Recordset['Fecha']; ?>"></td>
  8. <td><input name="Direccion_Url" type="text" id="Direccion_Url" value="<?php echo $row_Recordset['Direccion_Url']; ?>"></td>
  9. <td><input name="Hipervinculo" type="text" id="Hipervinculo" value="<?php echo $row_Recordset['Hipervinculo']; ?>"></td>
  10. <td><input name="Usuario" type="text" id="Usuario" value="<?php echo $row_Recordset['Usuario']; ?>"></td>
  11. <td><input name="Password" type="text" id="Password" value="<?php echo $row_Recordset['Password']; ?>"></td>
  12. </tr>
  13. </table>
  14. <?php } while ($row_Recordset = mysql_fetch_assoc($Recordset)); ?>

Alli imprimes varios input con el mismo nombre de variable, es por eso que cuando envias o haces el submit al recogerlos en el servidor solo capturas los ultimos valores.

No crees que mejor lo harias asi:


Código PHP:
Ver original
  1. <?php
  2. $i=0;
  3. do { ?>
  4. <table border="1">
  5. <tr>
  6. <td><input name="id<? echo $i;?>" type="text" id="id" value="<?php echo $row_Recordset['id']; ?>" size="5"></td>
  7. <td><input name="Nombre_de_la_Pagina<? echo $i;?>" type="text" id="Nombre_de_la_Pagina" value="<?php echo $row_Recordset['Nombre_de_la_Pagina']; ?>"></td>
  8. <td><input name="Descripcion<? echo $i;?>" type="text" id="Descripcion" value="<?php echo $row_Recordset['Descripcion']; ?>"></td>
  9. <td><input name="Fecha<? echo $i;?>" type="text" id="Fecha" value="<?php echo $row_Recordset['Fecha']; ?>"></td>
  10. <td><input name="Direccion_Url<? echo $i;?>" type="text" id="Direccion_Url" value="<?php echo $row_Recordset['Direccion_Url']; ?>"></td>
  11. <td><input name="Hipervinculo<? echo $i;?>" type="text" id="Hipervinculo" value="<?php echo $row_Recordset['Hipervinculo']; ?>"></td>
  12. <td><input name="Usuario<? echo $i;?>" type="text" id="Usuario" value="<?php echo $row_Recordset['Usuario']; ?>"></td>
  13. <td><input name="Password<? echo $i;?>" type="text" id="Password" value="<?php echo $row_Recordset['Password']; ?>"></td>
  14. </tr>
  15. </table>
  16. <?php
  17.     $i++;
  18. } while ($row_Recordset = mysql_fetch_assoc($Recordset)); ?>


Saludos
Gildus
__________________
.: Gildus :.
  #5 (permalink)  
Antiguo 13/06/2011, 00:00
Avatar de the_web_saint  
Fecha de Ingreso: mayo-2008
Ubicación: localhost/tierra/america/panama
Mensajes: 1.229
Antigüedad: 15 años, 11 meses
Puntos: 43
Respuesta: error al actualizar los datos

Si es cierto, muy buen observación gildus.

De paso se las pones en los id's para evitar tenerlos repetidos jeje.

Slds
__________________
..::The Saint::..
El pesimista se queja del viento; el optimista espera que cambie; el realista ajusta las velas.
  #6 (permalink)  
Antiguo 13/06/2011, 05:52
Usuario no validado
 
Fecha de Ingreso: enero-2011
Mensajes: 36
Antigüedad: 13 años, 3 meses
Puntos: 0
Exclamación Respuesta: error al actualizar los datos

bueno mi pana le puse lo que me dijiste pero ahora no me actualiza ni ultimo ni el primero ni nada!!!!
  #7 (permalink)  
Antiguo 13/06/2011, 07:29
Avatar de the_web_saint  
Fecha de Ingreso: mayo-2008
Ubicación: localhost/tierra/america/panama
Mensajes: 1.229
Antigüedad: 15 años, 11 meses
Puntos: 43
Respuesta: error al actualizar los datos

Hola,

Si haces lo que nuestro compañero te comenta debes enviar el valor del i para hacer un bucle y recorrerlo para su actualización.

También puedes hacer lo siguiente:
Código PHP:
Ver original
  1. <?php
  2. $i=0;
  3. do { ?>
  4. <table border="1">
  5. <tr>
  6. <td><input name="id[]>" type="text" id="id<? echo $i;?>" value="<?php echo $row_Recordset['id']; ?>" size="5"></td>
  7. <td><input name="Nombre_de_la_Pagina[]" type="text" id="Nombre_de_la_Pagina<? echo $i;?>" value="<?php echo $row_Recordset['Nombre_de_la_Pagina']; ?>"></td>
  8. <td><input name="Descripcion[]" type="text" id="Descripcion<? echo $i;?>" value="<?php echo $row_Recordset['Descripcion']; ?>"></td>
  9. <td><input name="Fecha[]" type="text" id="Fecha<? echo $i;?>" value="<?php echo $row_Recordset['Fecha']; ?>"></td>
  10. <td><input name="Direccion_Url[]" type="text" id="Direccion_Url<? echo $i;?>" value="<?php echo $row_Recordset['Direccion_Url']; ?>"></td>
  11. <td><input name="Hipervinculo[]" type="text" id="Hipervinculo<? echo $i;?>" value="<?php echo $row_Recordset['Hipervinculo']; ?>"></td>
  12. <td><input name="Usuario[]" type="text" id="Usuario<? echo $i;?>" value="<?php echo $row_Recordset['Usuario']; ?>"></td>
  13. <td><input name="Password[]" type="text" id="Password<? echo $i;?>" value="<?php echo $row_Recordset['Password']; ?>"></td>
  14. </tr>
  15. </table>
  16. <?php
  17.     $i++;
  18. } while ($row_Recordset = mysql_fetch_assoc($Recordset)); ?>

Cuando vas a actualizar colocas algo así:
Código PHP:
Ver original
  1. for($i=0; $i<count($_POST['id']); $i++)
  2. {
  3. $updateSQL = sprintf("UPDATE personal SET Nombre_de_la_Pagina=%s, Descripcion=%s, Fecha=%s, `Direccion_Url`=%s, Hipervinculo=%s, Usuario=%s, Password=%s WHERE id=%s",
  4. GetSQLValueString($_POST['Nombre_de_la_Pagina'], "text"),
  5. GetSQLValueString($_POST['Descripcion'], "text"),
  6. GetSQLValueString($_POST['Fecha'], "date"),
  7. GetSQLValueString($_POST['Direccion_Url'], "text"),
  8. GetSQLValueString($_POST['Hipervinculo'], "text"),
  9. GetSQLValueString($_POST['Usuario'], "text"),
  10. GetSQLValueString($_POST['Password'], "int"),
  11. GetSQLValueString($_POST['id'], "int"));
  12.  
  13. mysql_select_db($database_informacion, $informacion);
  14. $Result1 = mysql_query($updateSQL, $informacion) or die(mysql_error());
  15. }

Para que recorra los diferentes registros y puedas actualizarlos.

Slds
__________________
..::The Saint::..
El pesimista se queja del viento; el optimista espera que cambie; el realista ajusta las velas.

Etiquetas: php+mssql
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 02:18.