Your update code is after your email code. I'd start by moving the code from lines 166-206 up above to line 78.
Also it appears your update is updating from the FORM submit, but it is set into the session. So on line 177 you have:
$WA_fieldValuesStr = "".((($_POST["temppw"] != ""))?WA_HashEncryption($_POST["temppw"]):$row_SecurityAssistparausers["user_pass"]) ."";
I think it should probably be:
$WA_fieldValuesStr = "".WA_HashEncryption($_SESSION["randompw"]) ."";
I don't have a copy of the email body, but that should write the value:
<?php echo($_SESSION["randompw"]); ?>
for the temporary password as well.