gpt4 book ai didi

PHP 旧数据在 POST 后仍然显示

转载 作者:行者123 更新时间:2023-11-29 19:50:46 25 4
gpt4 key购买 nike

下面的代码有效,因为它正确更新了数据库。但是,帖子完成后,页面上的数据是旧的,即 $reader_busy[0] 不会更新,即使它是由 SELECT 查询驱动的。

要强制 $reader_busy[0] 在用户屏幕上正确更新,需要刷新。这意味着页面会在帖子发布后重新加载,然后刷新。

如何让页面显示来自 mysql 的正确数据,而不必加载页面两次?

我的 PHP 代码的简化版本:

<?php 
$reader_key = 'jm2';
$reader_busy_query = "SELECT `BusyWithClient` " .
"FROM {$wpdb->prefix}assistant " .
"WHERE `Key`='" . $reader_key . "'";

$reader_busy = $wpdb->get_col( $wpdb->prepare( $reader_busy_query ) );
$busy_with_client = $reader_busy[0];

if(isset($_POST['toggle'])){
$result = $wpdb->update(
$wpdb->prefix . 'assistant',
array('BusyWithClient' => $busy_with_client),
array('Key' => $reader_key)
);

// I want to remove / replace this line, and still see updated data.
echo "<meta http-equiv='refresh' content='0; url=" . curPageURL() . "#instructions" . "'>";
}
?>

<h1>
<?php print_r('busy: ' . $reader_busy[0]); ?>
<h1>

<form method="post" action="<?php echo curPageURL() . '#instructions'; ?>">
<input type="submit" name="toggle" value="toggle" >
</form>

最佳答案

只需颠倒你做事的顺序即可。不要先检索然后更新,而是先更新再检索。

<?php 
if (isset($_POST['toggle'])) {
$result = $wpdb->update(
$wpdb->prefix . 'assistant',
array('BusyWithClient' => $busy_with_client),
array('Key' => $reader_key)
);
}

$reader_busy_query = "SELECT `BusyWithClient` " .
"FROM {$wpdb->prefix}assistant " .
"WHERE `Key`='" . $reader_key . "'";

$reader_busy = $wpdb->get_col($wpdb->prepare($reader_busy_query));
?>

关于PHP 旧数据在 POST 后仍然显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40821939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com