unlink() in a for loop

They have: 164 posts

Joined: Nov 2001

hi, got a problem with unlink().

this is wat i want to do:
i search records then display it. if user click the check box beside the record, then click the delete button, the record will be deleted.

when i have one record, and i use unlink(), everything works fine. but if i click several records to be deleted, then unlink () failed. it didn't give me any errors. just simply didn't unlink the file.

any clues??

this is my unlink code:

<?php
if ($HTTP_POST_VARS['do'] == 'Delete')
    {
       
$pid = $HTTP_POST_VARS['pid'];
       
$cb1 = $HTTP_POST_VARS['cb'];
       
       
$sku = $HTTP_POST_VARS['sku'];
       
$track = $HTTP_POST_VARS['track'];
       
$brand = $HTTP_POST_VARS['brand'];
       
$price1 = $HTTP_POST_VARS['price1'];
       
$price2 = $HTTP_POST_VARS['price2'];
               
        for (
$i=0;$i<count($cb1); $i++)
        {   
           
$sel = mysql_query(\"select normal_image, bigger_image from product where pid = $cb1[$i]\");
            echo \"select normal_image, bigger_image from product where pid =
$cb1[$i]\";
            list (
$normal_image, $bigger_image) = mysql_fetch_array($sel);
$big = \"c:/inetpub/wwwroot/shop_cart/img/\".$bigger_image;
$normal = \"c:/inetpub/wwwroot/shop_cart/img/\".$normal_image;
           
unlink(
$big);
unlink(
$normal);
       
$sql = \"DELETE FROM product WHERE pid = $cb1[$i]\";
$result = mysql_query($sql);
}
search(\"
$sku\",\"$track\",\"$brand\",\"$price1\",\"$price2\");
}
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Try debuging... replace the unlink with echo.

You've also got no debug or error trap on those queries. I'm guessing it's those that are failing, and not the unlinks.

Also, you can't use the same variable ($sel) in a loop. Using it as a query resource identifier and later a query, looses the initial recordset set.

Try this:

<?php
function Query_Error($file, $line, $query, $silent=true)
{
   
/**
     * INPUTS:
     * str $file        - file where query was run
     * int $line        - aprox. line where query was run
     * str $query       - query that was run
     * bln $silent      - print bebug report in <!-- comments --> ?
     *
     * OUTPUTS:
     * Prints a detailed error report using $file, $line, $query,
     * and mysql_error(). 
     *
     * RETURN: (void)
     *
     * NOTES:
     * mysql_error() returns the mysql error generated by the last run query
     * (for the current DB link).
     *
     */
   
   
echo \"\n\n\";
    echo (
$silent == TRUE) ? \"<!--\n\" : \"<textarea cols=\\"60\\" rows=\\"7\\">\n\";
    echo \"FILE:
$file\n\";
    echo \"LINE:
$line\n\";
    echo \"\n\";
    echo \"QUERY ERROR:\n\";
    echo mysql_error() . \"\n\";
    echo \"\n\";
    echo \"QUERY:\n\";
    echo
$query . \"\n\";
    echo (
$silent == TRUE) ? \"-->\n\" : \"</textarea>\n\";
    echo \"\n\n\";
   
} //END Query_Error()




if (
$HTTP_POST_VARS['do'] == 'Delete')  {
   
$pid = $HTTP_POST_VARS['pid'];
   
$cb1 = $HTTP_POST_VARS['cb'];
   
   
$sku = $HTTP_POST_VARS['sku'];
   
$track = $HTTP_POST_VARS['track'];
   
$brand = $HTTP_POST_VARS['brand'];
   
$price1 = $HTTP_POST_VARS['price1'];
   
$price2 = $HTTP_POST_VARS['price2'];
   
    for (
$i=0;$i<count($cb1); $i++) {
       
$query = \"SELECT normal_image, bigger_image FROM product WHERE pid='$cb1[$i]'\"
       
$result = mysql_query($query);
        if (!
$result) { Query_Error(__FILE__, __LINE__, $query, FALSE); }
        list (
$normal_image, $bigger_image) = mysql_fetch_array($result);
       
$big = \"c:/inetpub/wwwroot/shop_cart/img/\".$bigger_image;
       
$normal = \"c:/inetpub/wwwroot/shop_cart/img/\".$normal_image;
       
        //unlink(
$big);
        //unlink(
$normal);
       
        echo \"unlink(
$big);\n\";
        echo \"unlink(
$normal);\n\";
       
       
$query = \"DELETE FROM product WHERE pid='$cb1[$i]'\";
       
$result2 = mysql_query($query);
        if (!
$result2) { Query_Error(__FILE__, __LINE__, $query, FALSE); }
    }
   
    search(\"
$sku\",\"$track\",\"$brand\",\"$price1\",\"$price2\");
}
?>

Can we see the HTML for the list of checkboxes? From what I see, the checkboxes should be named "cb1[X]". With "X" being an incrementing number starting at zero (0).

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 164 posts

Joined: Nov 2001

oh....now the unlink become like...say
i have 3 records...and i clicked 2 of it and click delete. i will see unlink error BUT i check the image folder, the file is not there. means it's arleady unlink but it prompt me error.

so after i click delete, it should bring me back to the page where i have checkbox. the problem here is i still remain one record rite? but now when i go back to tat page, it says i dun have anyrecords left.

and IF i have 3 records and i delete all of them, then i won't get the unlink problem. so any idea how am i going to solve it?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Boy, I was off on a few things in my last post. I miss read $sql as another $sel. I also totally missed this line:
$cb1 = $HTTP_POST_VARS['cb'];

Anyway, I'm still lost...
I can't help you until I know what's wrong.
What is the error that unlink gives you?
What is it trying to unlink that it gives you this error?

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.