Delete foto in database e cartella

  • Creatore Discussione Creatore Discussione Alex_70
  • Data di inizio Data di inizio
Intanto prova a cambiare così, forse il problema era che $actor_id diventa vuota dopo l'invio, dato che non vedo il parametro id nell'url della foto

esatto Tommy, il problema era proprio qui

PHP:
<a href="?id=<?php echo $actor_id;?>&deleteid=<?php echo $row["gallery_id"]?>" class="btn btn-primary">Delete image</a>
 
problemi :(

Codice:
header('location:gallery_delete.php?id=$actor_id&success=true');

sshot-1.png


sshot-2.png

l'url e' cambiato $actor_id non e' presente come 943
 
reinserito l'header precedente

Codice:
header('location:gallery_delete.php?success=true');

altro problema, questo perche' prima erano i test su una foto, adesso sono presenti 2

deleteid=949
deleteid=950

sshot-3.png


cancello deleteid=950

sshot-4.png


ricarico la pagina, e qui arriva la sorpresa, invece di cancellare la 950 ha cancellato la 949
la conferma arriva sotto
ma io visualizzo la 949, forse e' nella cache

sshot-5.png


vado a cancellare questa ma ovviamente ottengo errore perche' la 950 non esiste perche' e' stata eliminata prima,
il processo finisce

sshot-6.png


che casino :(
 
quindi nel database adesso e' rimasto 950 ma estata cancellata la foto nella cartella
nella cartella rimasta la foto 949 e sparita nel database
 
PHP:
$createDeletePath = "../image_upload/actor/uploads/$nome ($actor_id)/".$getIamgeName;
Questo spostalo sotto l'isset deleteid e mettici $_GET['deleteid'] al posto di $getImageName
 
PHP:
$createDeletePath = "../image_upload/actor/uploads/$nome ($actor_id)/".$getIamgeName;
Questo spostalo sotto l'isset deleteid e mettici $_GET['deleteid'] al posto di $getImageName

sotto la query?

e $getIamgeName = $row['photo'] lo cancello?

Codice:
$getIamgeName = $row['photo'];
      
        
        
        
        $createDeletePath = "../image_upload/actor/uploads/$nome ($actor_id)/".$getIamgeName;
    
        
    
    if(isset($_GET['deleteid']))
    {
        
        $selectSql = "
        
        select * from gallery WHERE actor_id_fk = ".$_GET['id'];
        $rsSelect = mysqli_query($conn,$selectSql);
        $getRow = mysqli_fetch_assoc($rsSelect);
 
PHP:
<?php 
    
    require_once("cineteca.include");

    if(!$conn)
    {
        die(mysqli_error());
    }
    
    $actor_id = $_GET["id"];
    
    
    
    $sql = "
select *
FROM 
actor a 
INNER JOIN
gallery g 
on 
g.actor_id_fk = a.actor_id
WHERE 
a.actor_id=". $actor_id;
    
    
    
    $rs = mysqli_query($conn, $sql);
    
    while($row = mysqli_fetch_assoc($rs))
        {
    
    $nome= $row ['nome'];
    
    $getIamgeName = $row['photo'];    
        
    
    if(isset($_GET['deleteid']))
    {
        $createDeletePath = "../image_upload/actor/uploads/$nome ($actor_id)/".$_GET['deleteid'];
        $selectSql = "
        
        select * from gallery WHERE actor_id_fk = ".$_GET['id'];
        $rsSelect = mysqli_query($conn,$selectSql);
        $getRow = mysqli_fetch_assoc($rsSelect);
        
        
        
        
        
        if(unlink($createDeletePath))
        {
            $deleteSql = "delete from gallery where gallery_id = ".$_GET['deleteid'];
            $rsDelete = mysqli_query($conn, $deleteSql);    
            
            if($rsDelete)
            {
                header('location:gallery_delete.php?success=true');
                exit();
            }
        }
        else
        {
            $errorMsg = "Unable to delete Image";
        }
}

    
?>

<!DOCTYPE html>
<html>
<head>
<title>PHP remove uploaded file from folder and database</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
</head>



<body>
    <div class="container">
        <h3 class="text-center">Delete Images</h3>
        
        <div class="row">
        
        <?php 
        if(isset($errorMsg))
        {
        ?>
            <div class="alert alert-danger">
                <?php 
                    echo $errorMsg;
                    unset($errorMsg);
                ?>
            </div>
        <?php 
        }
        ?>
        
        <?php 
        if(isset($_GET['success']) && $_GET['success'] == 'true')
        {
        ?>
            <div class="alert alert-success">
                <?php 
                    echo "Images has been deleted sucessfully";
                ?>
            </div>
        <?php 
        }
        ?>
        
        
    <?php 
        
            
            $imgWithPath = "../image_upload/actor/uploads/$nome ($actor_id)/".$row['photo'];
        ?>    
            
                
            <div class="col-md-12 text-center" style="margin-top:10px">
                <img src="<?php echo $imgWithPath ?>" width="100">
                <a href="?id=<?php echo $actor_id;?>&deleteid=<?php echo $row["gallery_id"]?>" class="btn btn-primary">Delete image</a>
            </div>
            
            
    <?php        
        
        }
    ?>
    </div>
    </div>
    
    
<script src="../js/bootstrap.min.js"></script>    
</body>
</html>
prova cosi
 
non va, perche' non trova l'immagine e abbiamo eliminato questa

Codice:
$getIamgeName = $row['photo'];

il percorso e' composto cosi

Alex_72efd9d45f9efb3257880f4cb714fcbf.jpg

quella in rosso e' la path della cartella

quindi


$nome (dalla tabella actor)
$actor_id (dalla tabella actor)
$photo (dalla tabella gallery)

Alex (40)/Alex_72efd9d45f9efb3257880f4cb714fcbf.jpg


sshot-1.png
 
si scusa ho sbagliato questa riga doveva essere cosi:
PHP:
$createDeletePath = "../image_upload/actor/uploads/$nome ($actor_id)/".$getIamgeName;
 
il problema e' associare ogni unlink al suo pulsante, ho provato questo ma non va

PHP:
<a href="?id=<?php echo $actor_id;?>&deleteid=<?php echo $row["gallery_id"]?><?php echo $createDeletePath;?>" class="btn btn-primary">Delete image</a>
 
PHP:
<?php 
   
require_once("cineteca.include");

if(!$conn)
{
die(mysqli_error());
}

$actor_id = $_GET["id"];



$sql = "
select *
FROM
actor a
INNER JOIN
gallery g
on
g.actor_id_fk = a.actor_id
WHERE
a.actor_id=". $actor_id;



$rs = mysqli_query($conn, $sql);

while($row = mysqli_fetch_assoc($rs))
{

$nome= $row ['nome'];

$getIamgeName = $row['photo'];


if(isset($_GET['deleteid']))
{
$selectSql = "

select * from gallery WHERE gallery_id= ".$_GET['deleteid'];
$rsSelect = mysqli_query($conn,$selectSql);
while($getRow = mysqli_fetch_assoc($rsSelect)){
$idok=$getRow['photo'];
}

$createDeletePath = "../image_upload/actor/uploads/$nome ($actor_id)/".$idok;



if(unlink($createDeletePath))
{
$deleteSql = "delete from gallery where gallery_id = ".$_GET['deleteid'];
$rsDelete = mysqli_query($conn, $deleteSql);

if($rsDelete)
{
header('location:gallery_delete.php?success=true');
exit();
}
}
else
{
$errorMsg = "Unable to delete Image";
}
}


?>

<!DOCTYPE html>
<html>
<head>
<title>PHP remove uploaded file from folder and database</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
</head>



<body>
    <div class="container">
        <h3 class="text-center">Delete Images</h3>
        
        <div class="row">
        
        <?php 
        if(isset($errorMsg))
{
?>
            <div class="alert alert-danger">
                <?php 
                    echo $errorMsg;
unset($errorMsg);
?>
            </div>
        <?php 
        }
?>
        
        <?php 
        if(isset($_GET['success']) && $_GET['success'] == 'true')
{
?>
            <div class="alert alert-success">
                <?php 
                    echo "Images has been deleted sucessfully";
?>
            </div>
        <?php 
        }
?>
        
        
    <?php 
       

$imgWithPath = "../image_upload/actor/uploads/$nome ($actor_id)/".$row['photo'];
?>    
            
                
            <div class="col-md-12 text-center" style="margin-top:10px">
                <img src="<?php echo $imgWithPath ?>" width="100">
                <a href="?id=<?php echo $actor_id;?>&deleteid=<?php echo $row["gallery_id"]?>" class="btn btn-primary">Delete image</a>
            </div>
            
            
    <?php        
       
}
?>
    </div>
    </div>
    
    
<script src="../js/bootstrap.min.js"></script>    
</body>
</html>
 

Discussioni simili