1

I have a web app which is basically one page with a single form broken down into tabs, each containing data from separate database tables. The form action points to a save.php script which takes the form data and saves it into the database.

The save.php script is structured so that it is composed of multiple SQL INSERT/UPDATE statements, one for each database table. It's done this way because some form tabs are hidden based on user preferences etc so a variable number of those statements will actually be run at any time.

Is there any way I can determine when all SQL queries have finished executing so that I can redirect the user to another page?

As a quick example, the save.php script is composed of several blocks of code which basically look like this:

 $updQuery = "UPDATE exampletable SET row=1 WHERE x=y";
 try { 
  $updResult = odbc_exec($connect,$updQuery); 
 } 
 catch (RuntimeException $e) { 
   ExceptionHandlingStuff();
 }

Normally for a redirect I'd just use a meta-refresh like so:

<meta http-equiv="refresh" content="10; URL=homepage.php">

I can set it to a long delay like 10 seconds to be 'reasonably' sure all the SQL queries will have executed by the time the refresh kicks in but this seems like a horrible hack that offers no guarantee and causes unnecessary delay for the user, because the queries will almost always have completed long before the 10 second delay is over.

What is the best way to detect when all SQL INSERT/UPDATE statements on a page have finished executing so that I can redirect the user immediately afterwards?

For reference I'm using PHP and ODBC to connect to a 4D SQL ANSI-92 database.

Tim Penner
  • 3,551
  • 21
  • 36
Roy
  • 705
  • 2
  • 11
  • 32

1 Answers1

2

That exec command is not run in parallel to other queries. This update is finished when you either catch an exception or fall to the next line of code after the catch block. So, given the limited information here, I would say redirect at the bottom of save.php like so:

$statusCode = ($statusCode ?: 200);
$statusCodeMessage = ($statusCodeMessage ?: '');    
$forwardingUrl = ($forwardingUrl ?: 'http://www.google.com/');
header(sprintf("HTTP/1.1 %d %s", $statusCode, $statusCodeMessage));    
header('Location: ' . $forwardingUrl);
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
jac2r
  • 31
  • 2