简体   繁体   English

在更新后直接检索记录时,PHP页面未显示来自MySQL的更新数据

[英]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). 我在PHP / MySQL Web应用程序中有一个简单的页面,可让管理员用户编辑HTML模板电子邮件(存储在数据库中)。 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. 提交表单后,将调用PHP函数,该函数通过对MySQL存储函数的参数化查询来更新数据库中的记录。

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); 添加以下行: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: 这是页面顶部的PHP代码:

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: 这是实际执行更新的EmailTemplate_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() 检索记录以供显示的函数有些复杂,但实际上只是使用mysqli fetch_object()将记录作为对象带回了。

Please help. 请帮忙。

hopefully you'll be using engine=innodb so you can use transactions: 希望您将使用engine = innodb这样可以使用事务:

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. 在尝试升级MySQL和PHP之后,由于各种依赖性问题,我设法中断了服务器上的Linux安装。 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. 我怀疑问题可能出在原始MySQL安装的my.cnf文件中,InnoDB设置行被注释掉了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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