简体   繁体   中英

PHP page not showing updated data from MySQL when record is retrieved directly after being updated

I have a simple page in a PHP/MySQL web application that lets admin users edit HTML template e-mails (stored in the database). Essentially to edit a template the user types into a textarea and submits a form which posts back to the same page.

When the form is submitted, a PHP function gets called that updates the record in the database by means of a parameterised query to a MySQL stored function.

Finally the record is retreived from the database and the information is displayed on the page - this step also happens when the page is being displayed for the first time.

The problem I am having is that although the record is being updated in the database before it is retrieved for display, the page is still displaying the old data from before the update when it is rendered. Other things on the page, such as the banner message saying the update was successful DO get updated, so I don't think it's a browser caching problem. Adding the line: sleep(1); after the record update solves the problem so the page shows the updated record, but for performance reasons this isn't ideal, and I don't understand why this problem is happening in the first place.

Here is the PHP code from the top of the page:

require_once('includes/global.php');
require_once('includes/emailtemplateutils.php');

$strMsg = '';

$lngEmailTemplatePK = ConvertToString($_GET['key'], 0);

if (ConvertToString($_POST['hdnSave']) == '1')
{

    $strFromAddress = ConvertToString($_POST['txtFromAddress']);
    $strSubjectLine = ConvertToString($_POST['txtSubjectLine']);
    $strBodyHTML = ConvertToString($_POST['txtBodyHTML']);

    if (EmailTemplate_Update($lngEmailTemplatePK, $strFromAddress, $strSubjectLine, $strBodyHTML))
    {

        $strMsg = FormatMsg('E-mail template updated.');

    }
    else
    {

        $strMsg = FormatErrorMsg('Could not save e-mail template!');

    }

}

$objEmailTemplate = EmailTemplate_Display($lngEmailTemplatePK);

This is the code of the EmailTemplate_Update() function which actually does the update:

function EmailTemplate_Update($lngEmailTemplatePK, $strFromAddress, $strSubjectLine, $strBodyHTML)
{

    $objConn = new mysqli(cstrGlobal_DbServer, cstrGlobal_DbUsername, cstrGlobal_DbPassword, cstrGlobal_DbCatalog);

    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $strQuery = "SELECT srf_EmailTemplate_Update(?, ?, ?, ?)";

    $objStmt = $objConn->prepare($strQuery);

    $objStmt->bind_param('isss', $lngEmailTemplatePK, EmptyToNull($strFromAddress), EmptyToNull($strSubjectLine), EmptyToNull($strBodyHTML));

    $objStmt->execute();

    $objStmt->bind_result($lngRetVal);

    $objStmt->fetch();

    $objStmt->close();

    $objConn->close();

    return $lngRetVal == 1;

}

The function for retrieving the record for display is a bit more convoluted but essentially just brings back the record as an object using mysqli fetch_object()

Please help.

hopefully you'll be using engine=innodb so you can use transactions:

try
{
    $conn = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);

    $conn->autocommit(FALSE); // start transaction

    do CRUD work...

    $conn->commit(); 
}
catch(exception $ex)
{
    $conn->rollback();
}

I eventually managed to solve this problem, though not in the way I would have planned. I managed to break the Linux install on the server due to various dependency issues after I attempted to upgrade MySQL and PHP. I ended up having to rebuild the server from scratch.

The code in my original question now works as expected. I suspect the issue may have been that the InnoDB setting lines were commented out in the my.cnf file of the original MySQL installation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM