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 Mar 28 2023, 02:02 PM
Post #2


Programming Fanatic
********

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



I was never able to figure out the actual problem. I did come up with a work around. I removed all 'img' tags and use PHP or JS to add them when needed.
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: 27th April 2024 - 03:05 PM