Foros del Web » Programando para Internet » PHP »

Eliminar registro

Estas en el tema de Eliminar registro en el foro de PHP en Foros del Web. Amigos...nesesito borrar un registro en la misma pagina donde se ve.......lo llamo con un SELECT FROM......y lo borro con un DELETE FROM.....pero me sale error ...
  #1 (permalink)  
Antiguo 15/12/2008, 20:43
Avatar de pablosky13  
Fecha de Ingreso: septiembre-2008
Mensajes: 42
Antigüedad: 15 años, 7 meses
Puntos: 0
Pregunta Eliminar registro

Amigos...nesesito borrar un registro en la misma pagina donde se ve.......lo llamo con un SELECT FROM......y lo borro con un DELETE FROM.....pero me sale error y no lo reconozco....
La idea es ver unas noticias en una tabla (meta lograda) que tengo en una base de datos y cada noticia tendria un boton "eliminar" (meta semi-lograda) ya que cada noticia tiene un texto eliminar que parece boton......solo que no funciona como tal.

Código PHP:
<?php
$conexion 
mysql_connect('localhost''pablo''pablo');
mysql_select_db('auto concept club');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$queEmp 
"SELECT * FROM noticias ORDER BY fecha DESC";
$resEmp mysql_query($queEmp$conexion) or die(mysql_error());
$totEmp mysql_num_rows($resEmp);

while (
$registro mysql_fetch_array($resEmp)) {
?>
<div id="tabla">
  <table width="410" border="0" cellpadding="1" cellspacing="4" bordercolor="#000000">
    <tr>
      <td width="35%" class="Estilo4"><?php echo $registro['fecha']; ?></td>
      <td width="26%">&nbsp;</td>
      <td width="20%">&nbsp;</td>
      <td width="19%">&nbsp;</td>
    </tr>
    <tr>
      <td class="Estilo1"><?php echo $registro['titulo']; ?></td>
      <td class="Estilo4"><?php echo $registro['zona']; ?></td>
      <td class="Estilo7"><div align="center">Modificar</div></td>
      <td class="Estilo6"><div align="center">Eliminar</div>
<?
$sSQL
="Delete From noticias Where titulo='$registro'";
$dilit=mysql_query($sSQL);
while (
$row=mysql_fetch_array($dilit))
mysql_free_result($dilit)
?> </td>
    </tr>
    <tr>
      <td colspan="4" class="Estilo2"><?php echo $registro['noticia_completa']; ?></td>
    </tr>
<?php
}
mysql_free_result($resEmp);
mysql_close($conexion);
?>
</table>
</div>
</body>
</html>
AYUDA PORFAAAAAAAAAAA
  #2 (permalink)  
Antiguo 15/12/2008, 21:50
 
Fecha de Ingreso: febrero-2008
Mensajes: 38
Antigüedad: 16 años, 2 meses
Puntos: 0
Respuesta: Eliminar registro

Hola me imagino que habras querido decir que hay un boton para eliminar, sencillo pones lo siguiente

OJO el siguiente codigo es para el boton eliminar!

Código:
<a onclick="willSubmit=confirm('&iquest;Esta seguro de eliminar este registro?'); return willSubmit;" 
href="nombre_de_tu_pagina?id_del=<?=$row['llave_primaria']?>" title="Eliminar">
<img src="ico_delete.gif"  border="0" alt="Eliminar"></a>


<br>

lo que dice este codigo es, enviame a "nombre_de_tu_pagina" y te mando el id del registro para que lo elimines,
todas las variables que se envian por url se reciben por $_GET
tons
al empezar la pagina pones

Código PHP:
if($_GET['id_del']) // esto dice si has pulsado el enlace eliminar ejecuta este codigo
/* aqui va tu consulta para eliminar */

$sSQL="Delete From noticias Where titulo='".$_GET['id_del']."'";
//$dilit=mysql_query($sSQL); no es necesario que asignes la consulta a una variable basta con...
mysql_query($sSQL);
/* esto no va esta por gusto, no estas consultando, 
estas eliminando (afectando a una tabla)
[B]while ($row=mysql_fetch_array($dilit))
mysql_free_result($dilit) [/B]*/


si no funciona imprime la consulta con un echo, copia y pegalo en tu phpmyadmin o mysqlbrowser y fijate si funciona.

Espero me cuentes como te fue. Te daria tu codigo en forma correcta pero no aprenderias nada, espero tus comentarios para ayudarte mas =P es por tu bien jeje php es el lenguaje mas facil yo lo aprendi en casa tengo 22 lo aprendi a los 20 =P
  #3 (permalink)  
Antiguo 17/12/2008, 11:15
Avatar de pablosky13  
Fecha de Ingreso: septiembre-2008
Mensajes: 42
Antigüedad: 15 años, 7 meses
Puntos: 0
Pregunta Respuesta: Eliminar registro

Muchas Gracias Ozkar_86, pero no me sirvió 100% ya que no supe que elementos reemplazar, pero si me ayudo a entender que hacer y llegue a este código el cual me aparece el botón eliminar, pero no cumple su función :S.
No se que hacer
Código PHP:
<?php
$conexion 
mysql_connect('localhost''pablo''pablo');
mysql_select_db('auto concept club');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$queEmp 
"SELECT * FROM noticias ORDER BY fecha DESC";
$resEmp mysql_query($queEmp$conexion) or die(mysql_error());
$totEmp mysql_num_rows($resEmp);

$btn_eliminar="<a href=tabla_noticias.php?eliminar=1>Eliminar</a>";

while (
$registro mysql_fetch_array($resEmp)) {

$ID=$registro['idnoticias'];

if (
eliminar==1)
$sSQL "Delete From noticias Where idnoticias = '$ID'"
?>
<div id="tabla">
  <table width="410" border="0" cellpadding="1" cellspacing="4" bordercolor="#000000">
    <tr>
      <td width="35%" class="Estilo4"><?php echo $registro['fecha']; ?></td>
      <td width="26%">&nbsp;</td>
      <td width="20%">&nbsp;</td>
      <td width="19%">&nbsp;</td>
    </tr>
    <tr>
      <td class="Estilo1"><?php echo $registro['titulo']; ?></td>
      <td class="Estilo4"><?php echo $registro['zona']; ?></td>
      <td class="Estilo7"><div align="center">Modificar</div></td>
      <td class="Estilo7"><? echo $btn_eliminar ?></td>
    </tr>
    <tr>
      <td colspan="4" class="Estilo2"><?php echo $registro['noticia_completa']; ?></td>
    </tr>
<?php
}
mysql_free_result($resEmp);
mysql_close($conexion);
?>
</table>
</div>
</body>
</html>
Gracias de todos modos

PD: tengo 23 años y llevo 5 meses aprendiendo php, se que no es difícil, pero hay cosas que ni se me ocurre como hacer, jajajajajajj, por eso recuro a este foro.


Siempre hay alguien que sabe mas que uno.
  #4 (permalink)  
Antiguo 17/12/2008, 23:32
 
Fecha de Ingreso: febrero-2008
Mensajes: 38
Antigüedad: 16 años, 2 meses
Puntos: 0
Respuesta: Eliminar registro

Código PHP:
<?php
$conexion 
mysql_connect('localhost''pablo''pablo');
mysql_select_db('auto concept club');

//siempre el codigo de las operaciones ponlo al principio para tener un orden
if($_GET['id_del']) // le pongo id_del por identificador de delete (borrado)
{
$sSQL="Delete From noticias Where idnoticias='".$_GET['id_del']."'";
mysql_query($sSQL);

// ojo estoy poniendo idnoticias como primary_key de tu tabla
}

?>
Código HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>:: Aprendiendo PHP =P</title>
</head>

<body>
<?php
$queEmp = "SELECT * FROM noticias ORDER BY fecha DESC";
$resEmp = mysql_query($queEmp, $conexion) or die(mysql_error());
$totEmp = mysql_num_rows($resEmp);


while ($registro = mysql_fetch_array($resEmp)) 
{

<div id="tabla">
  <table width="410" border="0" cellpadding="1" cellspacing="4" bordercolor="#000000">
    <tr>
      <td width="35%" class="Estilo4"><?php echo $registro['fecha']; ?></td>
      <td width="26%">&nbsp;</td>
      <td width="20%">&nbsp;</td>
      <td width="19%">&nbsp;</td>
    </tr>
    <tr>
      <td class="Estilo1"><?php echo $registro['titulo']; ?></td>
      <td class="Estilo4"><?php echo $registro['zona']; ?></td>
      <td class="Estilo7"><div align="center">Modificar</div></td>
      <td class="Estilo7"><a ref=tabla_noticias.php?id_del=<?php echo $idnoticias?>>Eliminar</a></td>
    </tr>
    <tr>
      <td colspan="4" class="Estilo2"><?php echo $registro['noticia_completa']; ?></td>
    </tr>
<?php
}
mysql_free_result($resEmp);
mysql_close($conexion);
?>
</table>
</div>
</body>
</html> 

Como sugerencia puedes ponerle a tu boton

Código HTML:
<a onclick="willSubmit=confirm('&iquest;Esta seguro de eliminar este registro?'); return willSubmit;" ref="tabla_noticias.php?id_del=<?php echo $idnoticias?>" title="Eliminar">Eliminar</a>
           
Errores cometidos

if (eliminar==1) // eliminar no es variable php, tuviste q haber puesto $eliminar ademas como es variable recibida tuvo que haber sido $_GET['eliminar'] ademas esta consulta se hace afuera espero funcione lo revise a la volada me avisas como te fue
$sSQL = "Delete From noticias Where idnoticias = '$ID'";
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 17:45.