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

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
", 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.

dpxyfa4718
dpxyfa4718 它可能是MySQL缓存问题...另外,你可以尝试直接输入查询到mysql解释
接近 10 年之前 回复

2个回答



我最终设法解决了这个问题,虽然不是我想要的方式。 在我尝试升级MySQL和PHP之后,由于各种依赖性问题,我设法破坏了服务器上的Linux安装。 我最终不得不从头开始重建服务器。</ p>

我原来问题中的代码现在按预期工作。 我怀疑问题可能是InnoDB设置行在原始MySQL安装的my.cnf文件中被注释掉了。</ p>
</ div>

展开原文

原文

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.

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();
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问