eliminar campos con checkbox
Publicado por Charly (70 intervenciones) el 05/02/2019 17:37:11
Tengo una tabla con varios mensajes de correo, y cada mensaje tiene un checkbox , para que al apretar un botón Eliminar que hay debajo de la tabla, se borren los marcados y se actualice la tabla.
La lista me aparece bien, pero no me borra los mensajes seleccionados.
Este es el código:
La lista me aparece bien, pero no me borra los mensajes seleccionados.
Este es el código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html>
<head>
<title>MENSAJES</title>
</head>
<body>
<?php
define("SERVIDOR","localhost");
define("USUARIO","root");
define("CLAVE","");
//define("BD","mensajes");
$BD="mensajes";
$db;
try{
if($BD!='')
$db=new PDO("mysql:host=".SERVIDOR.";dbname=".$BD.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
else
$db=new PDO("mysql:host=".SERVIDOR.";charset=utf8",USUARIO,CLAVE,array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES 'utf8'"));
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
$db->setAttribute(PDO::NULL_TO_STRING,true);
if($BD==''){
$sql=file_get_contents('mensajes.sql');
$this->ejecuta_SQL($sql);
}
}catch(PDOException $e){
die("<p><h3>No se ha podido establecer la conexión.
<p>Compruebe si está activado el servidor de bases de
datos MySQL.</h3></p>\n<p>Error: ".$e->getMessage()."</p>\n");
}
echo "<h1>LISTADO DE MENSAJES DE CORREO.</h1>
<p>Selecciona el 'asunto' de un mensaje para ver su contenido.</p>
<p>Maraca los que deseas borrar y pulsa el botón 'Eliminar'.</p>
<form name='form1' method='post' action=\"index.php\">";
$_POST['Eliminar']="";
$_POST['checkbox']="";
$sql1='SELECT id,de,asunto,fecha FROM mensajes';
$resultado1=$db->query($sql1);
echo "<table border=1>
<tr bgcolor='#FF0000'><th align='left'>De</th><th align='left'>Asunto</th><th align='left'>Fecha</th><th align='left'>Operaciones</th></tr>";
while(($fila1=$resultado1->fetch(PDO::FETCH_ASSOC))!=NULL){
echo "<tr bgcolor='#F78181'><td>".$fila1['de']."</td><td><a href='asunto.php?id'>".$fila1['asunto']."</td><td>".$fila1['fecha']."</td><td><input type='checkbox' name='checkbox[]' id='checkbox[]' value='".$fila1['id']."'></td>";
};
echo "<tr colspan='4' bgcolor='#F78181'><td colspan='4' align='right'><input type='submit' name='Eliminar' value='Eliminar'></td></tr>
</table></form>";
if(isset($_POST['Eliminar'])){
$borrar=implode(",",(array)$_POST['checkbox']);
$sql2="DELETE FROM mensajes WHERE id IN ($borrar)";
$resultado2=$db->query($sql2);
}
?>
</body>
</html>
Valora esta pregunta


1