Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Estou com um problema para deletar alguns registros em tabelas relacionadas e gostaria de uma ajuda se possivel. A estrutura do BD está assim:
CREATE TABLE gvnews (
id int(5) unsigned NOT NULL auto_increment,
titulo varchar(255) NOT NULL,
data date NOT NULL,
texto text,
PRIMARY KEY (id)
);
CREATE TABLE gvnews_img (
id int(5) unsigned NOT NULL auto_increment,
id_news varchar(5) NOT NULL,
imagem text,
PRIMARY KEY (id)
);
E o código que estou utilizando é este. Alguma coisa com certeza está feito errado na parte de deletar, pois o mesmo não funciona hehehehe. Ele tem que deletar o registro da noticia de GVNEWS e depois o registro relacionada a mesma noticia de GVNEWS_IMG e ai então remover a imagem que está armazenada em uma pasta. Qualquer ajuda ou diga será muito bem recebida :D
<?
include ("include/conecta.php");
require_once("verifica.php");
$id = $_GET["id"];
if($_GET["acao"] == excluir){
$consulta = mysql_query("SELECT * FROM gvnews WHERE id = '$id'");
$resultado = mysql_fetch_object($consulta);
$sql = "DELETE FROM gvnews,gvnews_img WHERE gvnews.id=gvnews_img.id_news AND gvnews.id ='$id' ";
$del = mysql_query($sql)or die(mysql_error());
if( $del ){
if(is_file('../' . $resultado->imagem)){
unlink('../' . $resultado->imagem);
}
}
}
?>
<!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">](http://www.w3.org/1999/xhtml)
<script language=javascript>
function abrir (URL){
window.open(URL,"janela1","width=450,height=500,left=400,top=200,scrollbars=YES")
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr>
<td><table width="600" border="0" cellspacing="0" cellpadding="0">
<?
$dados = mysql_query("SELECT * FROM gvnews ORDER BY data DESC");
$conta = mysql_num_rows($dados);
if($conta == "0"){
echo "Sem noticias cadastradas";
} else {
?>
<tr>
<td width="47">ID</td>
<td width="80">Data</td>
<td width="338">Titulo</td>
<td width="135">Status</td>
</tr>
<?
while($linha=mysql_fetch_array($dados)){
$id = $linha["id"];
$titulo = $linha["titulo"];
$data = $linha["data"];
$imagem = $linha["imagem"];
$data_explode = explode('-',$data);// Remove a / da variavel data
$data_explode[0];// Exibe somente o dia da data
$data_explode[1];// Exibe somente o mes da data
$data_explode[2];// Exibe somente o ano da data
$data_formatada = $data_explode[2]."/".$data_explode[1]."/".$data_explode[0];
?>
<tr>
<td><? echo "$id" ?></td>
<td><? echo "$data_formatada" ?></td>
<td><? echo "$titulo" ?></td>
<td><table width="100" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><a href="javascript:abrir('gvnews_view.php?id=<?php echo "$id"; ?>')">Visualizar</a></td>
<td><a href="?cat=gvnews⊂=editar&id=<?php echo "$id"; ?>">Editar</a></td>
<td><a href="?cat=gvnews&acao=excluir&id=<?php echo "$id"; ?>">Excluir</a></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?
}
}
?>
</table></td>
</tr>
</table>
</body>
</html>Carregando comentários...