I finally got this written and working except for reloading the form so that it shows the newly created copy. I have tried sc_exit, sc_redir, and tried redirecting to a blank app and back to the form, with no luck. To complicate things, I have no WHERE clause in the form’s SQL because I am using Application Link from a grid application to the form that has this copy button. How do I get the form to redirect back to the newly created copy?
//Make a temporary table and copy the current order fields into it.
$sqls = "CREATE TEMPORARY TABLE OrderTemp ENGINE=InnoDB " .
"SELECT * FROM Orders " .
"WHERE OrderID = " . {OrderID};
sc_exec_sql($sqls);
//Set the OrderID of the temporary record to NULL to avoid key field conflict.
$sqls = “UPDATE OrderTemp SET OrderID = NULL”;
sc_exec_sql($sqls);
sc_exec_sql(“start transaction;”);
//Add the temporary order record to the orders database.
$sqls = "INSERT INTO Orders " .
"SELECT * FROM OrderTemp ";
sc_exec_sql($sqls);
//Save the new OrderID to a variable
$NewOrderID = mysql_insert_id();
sc_exec_sql(“DROP TABLE OrderTemp”);//Remove temporary table.
//Make a temporary table and store the items of the copied order.
$sqls = "CREATE TEMPORARY TABLE ItemTemp ENGINE=InnoDB " .
"SELECT * FROM Items " .
"WHERE OrderID = " . {OrderID};
sc_exec_sql($sqls);
//Set OrderID of the temporary item table records to the new OrderID.
$sqls = "UPDATE ItemTemp SET OrderID = " . $NewOrderID ;
sc_exec_sql($sqls);
//Set the ItemID of the temporary record to NULL to avoid key field conflict.
$sqls = “UPDATE ItemTemp SET ItemID = NULL”;
sc_exec_sql($sqls);
//Add the temporary item records to the items database.
$sqls = "INSERT INTO Items " .
"SELECT * FROM ItemTemp ";
sc_exec_sql($sqls);
sc_exec_sql(“DROP TABLE ItemTemp”);//Remove temporary table.
sc_exec_sql(“commit;”);
//Redirect to the form for the new order
sc_redir(“B1-OrderForm”);