Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/03/2006, 05:45
Avatar de chiquirf
chiquirf
 
Fecha de Ingreso: noviembre-2005
Ubicación: Madrid
Mensajes: 215
Antigüedad: 18 años, 5 meses
Puntos: 3
Borrar registros de BD de una lista

Hola a todos, espero que me puedan ayudar.

Tengo un archivo llamado "prueba-lista.php" donde muestro dinámicamente todos los registros grabados en mi base de datos mediante una tabla, he agregado una columna con un botón de "borrar" al lado de cada registro para poder eliminar el registro que corresponda de la base de datos.

el enlace con la base de datos está perfecto, el problema es que al apretar cualquier botón de borrar me borra siempre el último registro y no el que corresponde.....

No se mucho de PHP pero he usado los ejemplos y comandos del Dreamweaver MX 2004 para hacerlo, he logrado generar la tabla, creado el formulario para agregar o editar cada registro, pero me falta la opción para eliminar el registro que yo quiera.

En el ejemplo de este foro tiene casi lo mismo, pero quiero que funcione como les explique arriba.

Adjunto el código del archivo "prueba-lista.php" para que lo vean y me ayuden.


Código:
<?php 
$hostname_procedimientos = "localhost"; 
$database_procedimientos = "pruebaphp"; 
$username_procedimientos = "carlos"; 
$password_procedimientos = "carlos";
$procedimientos = mysql_pconnect($hostname_procedimientos, $username_procedimientos, $password_procedimientos) or trigger_error(mysql_error(),E_USER_ERROR); 

mysql_select_db($database_procedimientos, $procedimientos);
$query_masivas = "SELECT * FROM masivas ORDER BY ID ASC";
$masivas = mysql_query($query_masivas, $procedimientos) or die(mysql_error());
$row_masivas = mysql_fetch_assoc($masivas);
$totalRows_masivas = mysql_num_rows($masivas);
?>
<?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;
}

// INICIO ELIMINAR REGISTRO........................

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if (   (isset($_POST["MM_del"]))    &&   ($_POST["MM_del"] == "form1")       ) {
 
 $delSQL = sprintf("DELETE FROM masivas WHERE ID=%s",
                       GetSQLValueString($_POST['ID'], "text")); 
/*  aqui creo que está el problema... no lee el ID correspondiente del imput */

  mysql_select_db($database_procedimientos, $procedimientos);
  $Result1 = mysql_query($delSQL, $procedimientos) or die(mysql_error());

  $updateGoTo = "masivas-lista.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
// FIN ELIMINAR REGISTRO........................
?>

<html>
<head>
<title>Intranet</title>
</head>
<body>
  <form name="form1" id="form1" method="post" action="<?php echo $editFormAction; ?>">
    <table border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#000000" style="border-collapse:collapse ">
      <tr>
        <th colspan="2" class="titulos02Negro" >INCIDENCIAS MASIVAS ACTIVAS </th>
      </tr>
      <tr align="center" class="negrita">
        <td>Ciudad</td>
        <td>Borrar</td>
      </tr>
      <?php do { ?>
      <tr>
        <td><?php echo $row_masivas['CIUDAD']; ?></td>
        <td align="center"><input name="Borrar<?php echo $row_masivas['ID']; ?>" type="submit" id="Borrar" value="Borrar" />
		<input type="hidden" name="MM_del" value="form1">
  <input type="hidden" name="ID" value="<?php echo $row_masivas['ID']; ?>">
  ID:<?php echo $row_masivas['ID']; ?>
	
		</td>
      </tr>
      <?php } while ($row_masivas = mysql_fetch_assoc($masivas)); ?>
    </table>
	  
  </form>
</body>
</html>
<?php
mysql_free_result($masivas);
?>

Espero que me puedan ayudar.....

Saludos y gracias por sus respuestas....

Carlos