I am trying to delete pictures from the server that are associated with a specific item, that way when the user clicks on the delete button it will delete the item as well as all pictures associated with it, after doing a bunch of searching and experimenting I have this code, I comented out all the code that deletes things in favor of just echoing each picture because I thought if I can get it to echo all the picture names then I would be able to uncomment the delete staments and allow it to delete, rather then having to constantly replace information on the server to test.

<?php
session_start();
include 'conection.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(isset($_SESSION['user_id']))
{
    $stock = $_POST['refStk'];
    $refId = $_POST['refID'];
    $numPics = 0;
    $pics = 0;

    $result = mysql_query("SELECT * FROM inventory WHERE id = '$refId'") or die(mysql_error());
    $row = mysql_fetch_array($result); 

    $picRes = mysql_query("SELECT * FROM uploads WHERE ref_id = '$stock'");

    if(isset($_POST['confirm']))
    {
        $stock = $_POST['refStk'];
        $id = $_POST['refID'];

        /*$query="DELETE FROM inventory WHERE id='".$id."'";
        $query_2="DELETE FROM uploads WHERE ref_id ='".$stock."'";
        if (!mysql_query($query,$con) || !mysql_query($query_2,$con))
        {
            die('Error: ' . mysql_error());
        }
        if(mysql_query($query,$con) && mysql_query($query_2,$con))
        {
            echo 'Vehicle Removed';
        }*/

        while($pics <= $numPics)
        {
            $delFile = 'pics/' . $_POST['picName'];

            echo $delFile;
            $pics++;
            /*if (file_exists($delFile))
            {
                //unlink($delFile);
                echo $delFile;
            }else
            {
                echo 'file does not exist';
            }*/
        }
    }
    ?>
    <!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=utf-8" />
    <title>File Delete</title>
    </head>
    <body onUnload="window.addEventListener("unload", invalidateBackCache, true)"><center>
    <table border = '1'>
    <tr>
    <th>Stock Number</th><th>Year</th><th>Make</th><th>Model</th><th>Mileage</th><th>Transmition</th><th>Body Style</th><th>Location</th><th>Asking Price</th>
    </tr>
    <tr>
    <td><?php echo $row['stock']?></td><td><?php echo $row['year']?></td><td><?php echo $row['make']?></td><td><?php echo $row['model']?></td><td><?php echo $row['mileage']?></td><td><?php echo $row['transmition']?></td><td><?php echo $row['body_style']?></td><td><?php echo $row['location']?></td><td><?php echo $row['ask_price']?></td>
    </tr>
    </table><br /><br />
    <form name="delete" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <?php
    while($picRow = mysql_fetch_array($picRes))
    {
        $numPics++
        ?>
        <img src="<?php echo "pics/".$picRow['name']; ?>" width="110" height="83" />
        <input type="hidden" name="picName" value="<?php echo $picRow['name']; ?>" />
        <?php
    }
    ?>
    <h1>Are you sure you want to delete all files associated with this vehicle?</h1>
    <input type="hidden" name="refID" value="<?php echo $refId ?>" />
    <input type="hidden" name="refStk" value="<?php echo $stock ?>" />
    <input type="submit" name="confirm" value="Yes" />
    <a href = vehicleSelect.php><input type="button" value="No" /></a>
    </form>
    </center>
    </body>
    </html>
    <?php
}else
{
    echo "<br /><br /><br /><br />";
    echo "<strong><center>"."You Are Not Authorized To view This Page......<a href='admin.php'>Please Login First !!</a>"."</center></strong>";
}
?>

all I get with this is the last picture posted, I have tried having each picture have its own form but then I errors trying to get the names to displayat all as if i am not even getting the information transmitted

Member Avatar for LastMitch

I am trying to delete pictures from the server that are associated with a specific item, that way when the user clicks on the delete button it will delete the item as well as all pictures associated with it, after doing a bunch of searching and experimenting I have this code, I comented out all the code that deletes things in favor of just echoing each picture because I thought if I can get it to echo all the picture names then I would be able to uncomment the delete staments and allow it to delete, rather then having to constantly replace information on the server to test.

I don't understand what you are trying to do?

The reason why I ask is because you don't have this file extension: "jpg", "jpeg", "gif", "png" for the images. How are you gonna delete that image?

Is this where your folder contain the images:

$delFile = 'pics/' . $_POST['picName'];

Base on what you have, the only way to delete that image is by id which you already establish.

Can you explain a little bit more what you trying to do?

I assume that you can delete a file but you want to delete more than 1 file?

The id is the best solution which you have.

exactly what you said, however I am trying to delete all the image files associated with the given product.

yes the pics/ is the folder that lods the pictures and the picName is the name of the image including the extention (.jpg, .gif, .png, etc.)

I can delete one picture at a time using similar code in the edit.php, and I am trying to adjust so that I can delete every picture, like when a product is removed from the inventory all together I want the pictures associated to get deleted with it. Accurate inventory you see.

I have even tried setting up a for loop, attaching $numPics to the name, however that has failed as well thus far

in order to use the loop method you need to fisrt have an array of you image file names. then you loop through them with a foreach to eliminate the unwanted photos. I have used this technique many times

$images=array('1.jpg', '2.jpg', '3.jpg');
foreach ($images as $image){
    unlink($image);
}

Or in your case I think it might be something like this

$picRes = mysql_query("SELECT * FROM uploads WHERE ref_id = '$stock'");
while($entry=mysql_fetch_assoc($picRes)){
    unlink($entry['imageUrlKey']);
    mysql_query("DELETE FROM uploads WHERE id='".$entry['id']."'");
}

how does that work with the post method I am using? Every time I try to post the paths to get to the multiple image files I get one posted and the rest are lost. Here is the updated code, which shows me where the hickup is at.

<?php
session_start();
include 'core/conection.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(isset($_SESSION['user_id']))
{
    $stock = $_POST['refStk'];
    $refId = $_POST['refID'];
    $numPics = 0;
    $picsA = array();

    $result = mysql_query("SELECT * FROM inventory WHERE id = '$refId'") or die(mysql_error());
    $row = mysql_fetch_array($result); 

    $picRes = mysql_query("SELECT * FROM uploads WHERE ref_id = '$stock'");

    if(isset($_POST['confirm']))
    {
        $stock = $_POST['refStk'];
        $id = $_POST['refID'];
        $numPics = $_POST['numPics'];

        /*$query="DELETE FROM inventory WHERE id='".$id."'";
        $query_2="DELETE FROM uploads WHERE ref_id ='".$stock."'";
        if (!mysql_query($query,$con) || !mysql_query($query_2,$con))
        {
            die('Error: ' . mysql_error());
        }
        if(mysql_query($query,$con) && mysql_query($query_2,$con))
        {
            echo 'Vehicle Removed <br />';
        }*/
        echo 'Stock Number: ' . $stock;
        echo '<br />';
        echo 'Id: ' . $id;
        echo '<br />';
        echo 'Picture Count: ' . $numPics;
        echo '<br /> <br />';
        print_r($picsA);

        /*for($i=0; $i < $numPics; $i++)
        {
            $delFile = 'pics/' . $_POST['picName'];

            echo $delFile;
            echo '<br />';
            if (file_exists($delFile))
            {
                //unlink($delFile);
                echo $delFile;
            }else
            {
                echo 'file does not exist';
            }
        }*/
        $numPics = 0;
    }
    ?>
    <!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=utf-8" />
    <title>File Delete</title>
    </head>
    <body onUnload="window.addEventListener("unload", invalidateBackCache, true)"><center>
    <form name="confirm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <table border = '1'>
    <tr>
    <th>Stock Number</th><th>Year</th><th>Make</th><th>Model</th><th>Mileage</th><th>Transmition</th><th>Body Style</th><th>Location</th><th>Asking Price</th>
    </tr>
    <tr>
    <td><?php echo $row['stock']?></td><td><?php echo $row['year']?></td><td><?php echo $row['make']?></td><td><?php echo $row['model']?></td><td><?php echo $row['mileage']?></td><td><?php echo $row['transmition']?></td><td><?php echo $row['body_style']?></td><td><?php echo $row['location']?></td><td><?php echo $row['ask_price']?></td>
    </tr>
    </table><br /><br />
    <table><tr>
    <?php
    $picCount = 0;

    while($picRow = mysql_fetch_array($picRes))
    {
        if($picCount >= 5)
        {
            echo '</tr><tr>';
            $picCount = 1;
            array_push($picsA, $picRow['name']);
            ?><td>
            <img src="<?php echo "pics/".$picRow['name']; ?>" width="110" height="83" />
            <input type="hidden" name="picName" value="<?php echo $picRow['name']; ?>" />
            </td><?php
            print_r($picsA);
            $numPics++;
        }else
        {
            $picCount++;
            array_push($picsA, $picRow['name']);
            ?><td>
            <img src="<?php echo "pics/".$picRow['name']; ?>" width="110" height="83" />
            <input type="hidden" name="picName" value="<?php echo $picRow['name']; ?>" />
            </td><?php
            print_r($picsA);
            $numPics++;
        }
    }
    ?></tr></table>
    <h1>Are you sure you want to delete all files associated with this vehicle?</h1>
    <input type="hidden" name="numPics" value="<?php echo $numPics?>" />
    <input type="hidden" name="refID" value="<?php echo $refId ?>" />
    <input type="hidden" name="refStk" value="<?php echo $stock ?>" /><?php print_r($picsA); ?>
    <input type="submit" name="confirm" value="Yes" />
    <a href = vehicleSelect.php><input type="button" value="No" /></a>
    </form>
    </center>
    </body>
    </html>
    <?php
}else
{
    echo "<br /><br /><br /><br />";
    echo "<strong><center>"."You Are Not Authorized To view This Page......<a href='admin.php'>Please Login First !!</a>"."</center></strong>";
}
?>

what gets me about this is from what I learned going through my classes, an array holds the values it is given untill they are cleared out either manually or by the program being turned off or restarted, I am not sure why the page is apparently reloading and therefore emptying the array of the values I had in it.

ok I got it working, just had to work with the array I had created inside the form and have it pass each name properly, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.