我似乎無法在 stackoverflow 中找到有效的解決方案。我希望使用表單帖子中提交的更新資料來更新表單值。資料庫更新,但重繪 后,值仍然相同。任何幫助,將不勝感激。謝謝!
<?php
global $wpdb;
$user_id = get_current_user_id();
$org_id = get_user_meta($user_id, '_org_id', true);
$orgs = $wpdb->get_results("select * from wp_organization");
$user_org = null;
foreach($orgs as $struct) {
if ($org_id == $struct->id) {
$user_org = $struct;
break;
}
}
?>
<?php
$connection = mysqli_connect("address", "login", 'password');
$db = mysqli_select_db($connection, 'databasename');
if(isset($_POST['update'])) {
$id = $org_id;
$query = "UPDATE `wp_organization` SET
name = '$_POST[org_name]',
shortname = '$_POST[shortname]',
industry = '$_POST[industry]',
description = '$_POST[description]'
where id = $id ";
$query_run = mysqli_query($connection, $query);
}
}
?>
<!-- organization info form -->
<form class="py-4 col-md-6 mx-auto" method="post" action="">
<h2 class="mb-3 text-center">Organization Information</h2>
<label class="form-label">Name</label>
<input type="text" required name="org_name" value="<?php echo $user_org-> name; ?>"
class="form-control mb-3" />
<label class="form-label">Industry</label>
<input type="text" name="industry" value="<?php echo $user_org-> industry; ?>" class="form-control mb-3" />
<label class="form-label">Short name (4 characters)</label>
<input type="text" name="shortname" required maxlength="4" value="<?php echo $user_org-> shortname; ?>" class="form-control mb-3" />
<label class="form-label">Description</label>
<textarea class="form-control mb-3" name="description" rows="5" cols="50"><?php echo trim(stripslashes($user_org-> description)); ?></textarea>
<div class="d-flex justify-content-center">
<input type="submit" name="update" value='Update' class='btn btn-primary'>
</div>
</form>
uj5u.com熱心網友回復:
導致舊資料出現的主要問題是您在更新資料庫之前正在查詢資料庫。表單中顯示的資料在頁面執行開始時被查詢,之后如果有 POST 資料,則更新資料庫。因此,用于呈現表單的資料是舊資料,而不是最近更新的資料。因此,以一種非常簡單的方式,您可以簡單地使用資料庫更新切換資料庫查詢,并且您的資料流將按預期作業。
但是,此代碼還有其他幾個問題,最值得注意的是 SQL 攻擊漏洞。因此,我將在為這個問題創建一個完整且安全的答案時解決所有這些問題。
- 這是WordPress!
根據$wpdb變數的存在以及一些表名,它似乎作為 WordPress 站點的一部分運行(或者與 WordPress 安裝一起運行)。因此,您永遠不需要進行任何原始mysqli_呼叫。相反,請使用類中記錄$wpdb的方法,這些方法提供對資料庫的安全訪問,并且還有助于確保將來對 WordPress 資料庫系統的任何更改都不會破壞您的代碼。
結果:洗掉mysqli_方法。
- 注意不必要的語法
有一些不必要的語法可能會產生不良的副作用。例如,在您擁有的代碼中間
?>
<?php
這個代碼片段實際上是在頁面的輸出中插入一個幻像空格(在這種情況下是一個文字換行符或\n)。例如,如果你試圖在這個值之后設定一個標題,你會得到一個關于輸出已經發送的困難而神秘的錯誤。一個好的做法是,您的所有業務代碼都位于一個連續的塊中,<?php檔案頂部只有一個塊。
此外,您的塊if(isset($_POST['update']))以一個額外的懸垂的右括號結尾}。
此外,在您的實際更新部分中,對超全域的呼叫$_POST不是實際的字串,而是原始標記(例如$_POST[shortname]。我懷疑您這樣做是為了利用 PHP 字串插值。雖然這種原始格式確實出于遺留原因,但它是插入這樣的字串以在字串內部使用類似的東西時,會更加清晰和安全{$_POST['shortname']}。
結果:洗掉并清理不必要的語法。
- SQL注入!天啊!
這段代碼中最可怕的一點是開放的 SQL 注入攻擊,它通過將不受信任的字串(以$_POST變數的形式)傳遞給 SQL 來啟用。這是成千上萬(如果不是數百萬)網站的禍根。任何可以訪問您的網頁的人都可以將他們想要的任何資料傳遞到表單欄位中,包括可以洗掉您的資料庫并破壞您的服務器的 SQL 命令。像這樣使用原始 SQL 是非常危險的,永遠不應該使用。PHPPDO為此包含了一個很棒的庫,還有許多其他選項。但是,如上所述,由于您使用的是 WordPress,因此您可以只使用$wpdb,它還具有內置的 SQL 注入保護功能,并提供與 WordPress 資料庫的進一步兼容性優勢。
結果:不要使用原始 SQL
- 更新然后查詢(但也先查詢)
導致您發布問題的核心問題是提交表單時網頁中的資料沒有更新,直到下一次加載。這是因為您當前從資料庫中查詢資料,然后更新資料庫。由于您使用早期查詢的結果在網頁中顯示值,因此它們自然不是新值。
我懷疑您這樣做是因為您需要 的值$org_id才能創建和查詢,結果您的所有查詢都聚集在一起。盡管如此,這個主要的 fetch$org_id需要在開始時,但在更新之后的其余查詢。
結果:修復查詢和更新排序。
- 無資料驗證
你盲目地使用 values 的$_POST值,但這些可能沒有設定。在您的示例中,這可能只會導致空資料庫值,但在其他情況下,這可能是災難性的。如上所述,任何人都可以使用$_POST他們想要的任何資料請求您的頁面。因此,最好確保您提供默認值,以防發送到服務器的資料缺少某些欄位。
結果:驗證是否存在所有預期資料。
此代碼的一個版本在查詢之前更新資料庫,因此每次頁面加載和更新時資料都是正確的,以及合并提到的其他修復,可能看起來像這樣:
<?php
/// 1. Get the global WordPress Database Object
global $wpdb;
/// 2. Use WP helper functiosn to query the current user and org IDs
$user_id = get_current_user_id();
$org_id = get_user_meta($user_id, '_org_id', true);
/// 3. Update the database if necessary
if (isset($_POST['update'])) {
/// 3.i Prepare the variables
// Note: In new versions of PHP, each of the isset ? : lines below could be replaced
// with something like:
//
// $name = $_POST['org_name'] ?? "default org name";
//
// I've used the long version here, so you can really see what ?? means. You can also
// put any value you want in the final string of these expressions, so for example
//
// $name = isset($_POST['org_name']) ? $_POST['org_name'] : "unknown";
//
// would provide the value "unknown" into the database. You could inline all of these
// checks into the following call to $wpdb, but that would make for a very hard to read
// line. Setting local variables to these values makes the code easier to follow.
$name = isset($_POST['org_name']) ? $_POST['org_name'] : "";
$short = isset($_POST['shortname']) ? $_POST['shortname'] : "";
$ind = isset($_POST['industry']) ? $_POST['industry'] : "";
$desc = isset($_POST['description']) ? $_POST['description'] : "";
/// 3.ii Execute the query
// Note: I assume that $org_id is an integer. If it's actually a string, then that
// line in the query should read
//
// where id = %s
$wpdb->query(
$wpdb->prepare("
UPDATE `wp_organization` SET
name = %s,
shortname = %s,
industry = %s,
description = %s
where id = %d
",
$name, $short, $ind, $desc, $org_id
)
);
// Alternatively to the raw $wpdb->query above, you could use the built in update method
// of $wpdb, like so
//
// $wpdb->update('wp_organization', array(
// 'name' => $name,
// 'shortname' => $short,
// 'industry' => $ind,
// 'description' => $desc,
// ),
// array( 'id' => $org_id ),
// array('%s', '%s', '%s', '%s'),
// array('%d')
// );
//
// This has the advantage that you don't have to write any SQL, but may be harder to understand
// if you come back to maintain this code far in the future.
}
/// 4. Load the current value of $user_org, this will reflect any database changes made above
$orgs = $wpdb->get_results("SELECT * FROM `wp_organization`");
$user_org = null;
foreach($orgs as $struct) {
if ($org_id == $struct->id) {
$user_org = $struct;
break;
}
}
/// 5. Render the form
?>
<!-- organization info form -->
<form class="py-4 col-md-6 mx-auto" method="post" action="">
<h2 class="mb-3 text-center">Organization Information
<label class="form-label">Name
<input type="text" required name="org_name" value="<?php echo $user_org->name; ?>" class="form-control mb-3" />
<label class="form-label">Industry
<input type="text" name="industry" value="<?php echo $user_org->industry; ?>" class="form-control mb-3" />
<label class="form-label">Short name (4 characters)
<input type="text" name="shortname" required maxlength="4" value="<?php echo $user_org->shortname; ?>" class="form-control mb-3" />
<label class="form-label">Description
<textarea class="form-control mb-3" name="description" rows="5" cols="50"><?php echo trim(stripslashes($user_org->description)); ?>
<div class="d-flex justify-content-center">
<input type="submit" name="update" value='Update' class='btn btn-primary'>
</div>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498202.html
下一篇:一個表格分布在多個頁面上
