下面的代碼可以正常工作,因爲它可以正確更新數據庫。然而,在帖子完成後,頁面上的數據是舊的,即使它是由SELECT查詢驅動的,$reader_busy[0]
也不會更新。PHP舊數據在POST後仍然顯示
要強制$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>
附註:您沒有正確使用準備好的語句。按照[示例](https://developer.wordpress.org/reference/classes/wpdb/prepare/#description)並使用佔位符。 – Mikey
現在你在Select之後設置$ read_key,它應該在它之前。另外Mikey是正確的,使用準備好的sql語句。 – Kainax