- Código: Selecionar tudo
CREATE TABLE `checkbox` (
`id_check` int(11) NOT NULL AUTO_INCREMENT,
`nome_check` varchar(255) DEFAULT NULL,
`descricao_check` text,
PRIMARY KEY (`id_check`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
Página de conexão com o Banco de Dados MySQL:
conexao.php
- Código: Selecionar tudo
<?php
$conexao = mysql_connect("localhost", "root", "");
$banco = mysql_select_db("tutorial");
?>
Página index.php, onde irá ser feita a seleção do campos e a exclusão dos mesmos.
- Código: Selecionar tudo
<?php
require("conexao.php");
?>
<!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=iso-8859-1" />
<title>Excluindo Vários Registros com Checkbox</title>
</head>
<body>
<h1>Excluindo vários Registros com CHECKBOX</h1>
<?php
$SQL = "SELECT * FROM checkbox";
$executa = mysql_query($SQL);
if(isset($_POST['excluir'])){
if(!isset($_POST['check'])){
echo "<script type='text/javascript'>alert('Selecione um registro!'); window.location.href='index.php'</script>";
}else{
$check = $_POST['check'];
foreach($check as $valor){
$DEL = "DELETE FROM checkbox WHERE id_check = '$valor'";
$qr = mysql_query($DEL);
echo "<script type='text/javascript'>alert('Registros excluidos com sucesso!'); window.location.href='index.php'</script>";
}
}
}
echo '<form action="" method="post" name="formularioCheckbox">';
while($exibir = mysql_fetch_assoc($executa)){
echo 'Nome: '.$exibir['nome_check'].'<br />';
echo 'Descrição: <p>'.$exibir['descricao_check'].'</p>';
echo "<input type='checkbox' name='check[]' value=".$exibir['id_check']." />";
echo '<hr />';
}
echo '<input type="submit" name="excluir" value="Deletar" />';
echo '</form>';
?>
</body>
</html>
