Whyyyyyyyyyyyy?(problem with unlink)<

liunx

Guest
why the image is deleted and not the entry in the database without clicking on the button?

in others words, the file on the hard drive is deleted when the form is not even submitted.


Function EffacerImage($NomImage,$db)
{
$QueryDelete = "DELETE FROM ".$_GET["Section"]." WHERE Images=".$NomImage;
mysql_query($QueryDelete,$db);
unlink($NomImage);

return ($_SERVER['PHP_SELF']."?id=1&Section=".$_GET["Section"]);
}

//Section admin pour effacer les photos
if (isset($_GLOBALS['kookie']))
{
$Action = EffacerImage($myrow["Images"],$db);
printf("<form name=\"Delete\" method=\"post\" enctype=\"multipart/form-data\" action=\"%s\">",$Action);
echo "<p align=\"center\">";
echo "<input name=\"delete\" type=\"submit\" value=\"effacer\">";
echo "</p>";
echo "</form>";
}simple...you need to do something like:


if($_POST["submit"])
{
//delete image
}
else
{
//display form
}but why the entry in te database isn't deleted?

also do i have to make a masqued label to have a value or the button itself submit something?I just realized your form won't work..you can't have action equal to a php function..use something like:
<form action="<? echo $_SERVER["PHP_SELF"]; ?>" method="post">


and yes...you have to name the submit button submit in order to have it submit something and have the php script detect it,unless you change the value of if($_POST["submit"]) to if($_POST["buttoname"]).Also $_GET["Section"] won't be set when you submit the form with POST...so use:

<input type="hidden" name="Section" value="<? echo $_GET["Section"]; ?>">

and use $_POST["Section"] in the query.Yeah, i figured out that it would not work and i changed it, but does the if($_POST["submit"]) will detect if the form was posted even if there is no field in the form exept the button?it should...php doesn't differenciate between what variables are passed...as long as it's passed it should work.yeah the function is running before the form is submittedOriginally posted by scoutt
yeah the function is running before the form is submitted

so you concur with my diagnosis,Dr. Scoutt?good good.You must join me for a Jolt and a Jumbo Meat Sub..yes yes...hehsounds good ot me, been years since I had jolt. count me in :POriginally posted by scoutt
sounds good ot me, been years since I had jolt. count me in :P

heh...sure thing...if you ever drag your american...ahem...fur over to canadian soil :DIt's not working. :crying:

maybe i'm doing something wrong, but I tried different things and i can't detect correctly when the form is submitted with only a submit button.

any idea what's wrong?


//Section admin pour effacer les photos
if (isset($_GLOBALS['kookie']))
{
//marche pas s'tafaire la-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-_*_-
if(isset($_POST["delete"]))
{
$Action = EffacerImage($myrow["Images"],$db);
}
else
{
$Action = "";
}
printf("<form name=\"Delete\" method=\"post\" enctype=\"multipart/form-data\" action=\"%s\">",$Action);
echo "<p align=\"center\">";
echo "<input name=\"delete\" type=\"submit\" value=\"effacer\">";
echo "</p>";
echo "</form>";
}I think you are missing the point. that code will not give an action to the form if the form has not been submitted. therefore you can't submit it to do the deletion.

you have to give the form a action so they can submit and then you check for the submit is set. don't have the function return the action variable if you don't want the deletion done before the form is submitted.

Function EffacerImage($NomImage,$db)
{
$QueryDelete = "DELETE FROM ".$_GET["Section"]." WHERE Images=".$NomImage;
mysql_query($QueryDelete,$db);
unlink($NomImage);

return "Deleted Ok";
}

//Section admin pour effacer les photos
if (isset($_GLOBALS['kookie']))
{
if(isset($_POST["delete"]))
{
echo EffacerImage($myrow["Images"],$db);
}
else
{

printf("<form name=\"Delete\" method=\"post\" enctype=\"multipart/form-data\" action=\"%s\">","$_SERVER['PHP_SELF']."?id=1&Section=".$_GET["Section"]");
echo "<p align=\"center\">";
echo "<input name=\"delete\" type=\"submit\" value=\"effacer\">";
echo "</p>";
echo "</form>";
}
}You were right... again :)
I still have some problems to adapt to the web programming.

Now, I have a new question: How can I know if the unlink fail?

Here is the code now(it's not perfect yet, cause the display of the image doesn't change, but I will figured out how to do this later.):

Function EffacerImage($NomImage,$NomTable,$db)
{
//DELETE FROM Cerf_de_Virginie WHERE Images='imagesCerf/Cerf01.jpg';
$QueryDelete = "DELETE FROM ".$NomTable." WHERE Images='".$NomImage."'";
mysql_query($QueryDelete,$db) or Die("Une erreur est survenue lors de la suppression de l'image dans la base de donn闁憇. Veuillez contacter l'administrateur de la base de donn闁憇.");
unlink($NomImage);

return "L'image ?闁犻枒 supprim闁
 
Back
Top