The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Need help with a mysterious problem
CharlesEF
post Feb 22 2023, 08:13 PM
Post #1


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Hi All,

I call this a mysterious problem because I'm baffled. Let me give you some background. I've been asked to update some old code to allow images to be posted with records that have no type of record code. Ex: ISO countries all have 2 or 3 character alpha codes (US, MX... etc) and a 3 digit numeric code. I still store all images on the HD, not in the DB. The actual path is in the app itself, depending on the record type. I only store the file name in the DB.

I decided to store images based on the record id. When a new record is added and images are uploaded for it, I create a temporary directory to hold the images, since a new record has no id yet (to be renamed when the record is saved). If the user clicks the CANCEL button or changes to a different page without saving the new record I need to delete the temporary directory. Sounds easy enough. To accomplish this I added the else portion to an existing function.

CODE
function checkLoginStatus($post)
{
if(!isset($_SESSION["user"]["id"]))
{
  addErrorMessage("login", "You must log in before you can access our database.");
  redirectToURL("/appl/");
} // Closing brace for if(!isset($_SESSION["user"]["id"]))
else
{
echo("post: {$post}" . PHP_EOL);
  if((bool)$post && !empty($_SESSION["user"]["upload"]["bpath"]) && !empty($_SESSION["user"]["upload"]["newdir"]))
  {
   if(file_exists("{$_SERVER['DOCUMENT_ROOT']}{$_SESSION['user']['upload']['bpath']}/{$_SESSION['user']['upload']['newdir']}"))
   {
echo("delete" . PHP_EOL);
    deleteDirectory("{$_SERVER['DOCUMENT_ROOT']}{$_SESSION['user']['upload']['bpath']}/{$_SESSION['user']['upload']['newdir']}");
   } // Closing brace for if(file_exists("{$_SERVER['DOCUMENT_ROOT']}{$_SESSION['user']['upload']['bpath']}/{$_SESSION['user']['upload']['newdir']}"))
   unset($_SESSION["user"]["upload"]);
  } // Closing brace for if((bool)$post && !empty($_SESSION["user"]["upload"]["bpath"]) && !empty($_SESSION["user"]["upload"]["newdir"]))
} // Closing brace for if(!isset($_SESSION["user"]["id"])) else
}

The first echo shows me the value passed in to the function. The 2nd echo shows 'delete' when the directory is removed.

CODE
checkLoginStatus(empty($_POST) ? 1 : 0);

This is how the function is called. If the $_POST array is empty then a 1 is passed. Otherwise a 0 is passed. This handles the problem when CANCEL is clicked or the page is changed without saving the new record.

Now the problem I found.
When a new record is saved and both client and server side validation both pass but MySQL reports on error (say duplicate key). I see 'post: 0' with no 'delete' but the actual temporary directory is removed. If I comment out the 'deleteDirectory' line in the function and run the test again I see 'post: 0' with no 'delete' and the temporary directory is still there.

How is the 'else' portion being run when 0 is passed to the function?

If you need any more info just ask.

This post has been edited by CharlesEF: Feb 22 2023, 08:16 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
CharlesEF
post Feb 27 2023, 03:41 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I must admit I'm still baffled but let me report what I've found.

The problem was not in the PHP code, it was in the HTML markup. This line:
CODE
<img id="iflag" class="<?php echo((!empty($_POST["id"]) && !empty($_POST["flag"]) && empty($validator->errors["flag"])) ? "img" : "hide");?>" style="border: 1px solid #000000; width: 75%;" src="<?php echo(!empty($_POST["flag"]) ? "{$_POST['bpath']}/" . (!empty($_SESSION["user"]["upload"]["newdir"]) ? $_SESSION["user"]["upload"]["newdir"] : $_POST["id"]) . "/{$_POST['flag']}" : "#");?>" alt="<?php if(!empty($_POST["cname"])) echo("{$_POST["cname"]} flag - front");?>">

Thee are 8 flag name inputs and 8 flag img's to show the flag. If no flag name is set then it is hidden. If I fill in all 8 flag names and submit then the temp directory is not deleted. If I fill in only 1 flag name then the temp directory is deleted. If I comment out the 'deleteDirectory' command in the function itself then the temp directory is not deleted. The function logic has shown that the 'deleteDirectory' section is skipped so why? Commenting out the command is not a valid option. This brings me back to the img tag.

If a flag name is given the markup is:
CODE
<img id="iflag" class="img" style="border: 1px solid #000000; width: 75%;" src="/appl_files/images/flags/2508834891/flag.png" alt="Testing flag - front">

If no flag name is given then the markup is:
CODE
<img id="iflagr" class="hide" style="border: 1px solid #000000; width: 75%;" src="#" alt="Testing flag - reverse">


Is there something special about the img tag I don't know about?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 27 2023, 04:18 PM
Post #3


.
********

Group: WDG Moderators
Posts: 9,679
Joined: 10-August 06
Member No.: 7



QUOTE(CharlesEF @ Feb 27 2023, 09:41 PM) *

If I fill in all 8 flag names and submit then the temp directory is not deleted. If I fill in only 1 flag name then the temp directory is deleted.

That sounds more like it's the flag form fields that are the cause, not the images? What happens if you fill on 2 to 7 flag names?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Feb 27 2023, 07:31 PM
Post #4


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Christian J @ Feb 27 2023, 03:18 PM) *

QUOTE(CharlesEF @ Feb 27 2023, 09:41 PM) *

If I fill in all 8 flag names and submit then the temp directory is not deleted. If I fill in only 1 flag name then the temp directory is deleted.

That sounds more like it's the flag form fields that are the cause, not the images? What happens if you fill on 2 to 7 flag names?

If I fill in any less than 8 and submit, to get the MySQL error: duplicate key, then the temp directory is deleted. If I fill in 1, 2, 3, 4, 5, 6 or 7 flag names then the temp directory is deleted. The page redraws showing all the correct information except the flag images, because the temp directory is gone.

This is the flag name markup:
CODE
    <div class="container" title="Please enter the country flag file name.">
     <label for="flag">Flag:</label><br>
     <input type="text" id="flag" name="flag" maxlength="50" onchange="java script:doesImageExist(this, '<?php echo($help_name);?>', '<?php echo(generateToken("$help_name " . $phrases[$_SESSION[$help_name]["phrase"]] . " $help_name", $secrets[$_SESSION[$help_name]["secret"]], false));?>');" value="<?php if(isset($_POST["flag"])) echo($_POST["flag"]);?>"><br>
     <span id="cefiform_flag_errorloc" class="error"><?php if(isset($validator->errors["flag"])) echo(implode("<br>", $validator->errors["flag"]));?></span>
    </div>


I don't see how any HTML markup could cause the temp directory to be deleted.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 27 2023, 08:28 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,679
Joined: 10-August 06
Member No.: 7



Do you mean you have eight identical INPUT elements like this in the same form:

CODE
<input type="text" id="flag" name="flag"

--all with the same ID and NAME value? unsure.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Feb 28 2023, 02:59 PM
Post #6


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(Christian J @ Feb 27 2023, 07:28 PM) *

Do you mean you have eight identical INPUT elements like this in the same form:

CODE
<input type="text" id="flag" name="flag"

--all with the same ID and NAME value? unsure.gif

No, they all have different ID and NAME values. I only showed the first.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 3rd June 2024 - 06:56 PM