[英]Update/insert only a column of database table in PHP
我的 WordPress 表名為 wp_fes_vendors,一共有 12 列。
在這 12 列中,我只想更新列名請求的數據
我為此列獲取的數據來自存儲在$_POST['getval']
中的后端自定義表單
這是我試過的代碼
if (!empty(isset($_POST['getval'])))
{
global $wpdb;
$table_name=$wpdb->prefix.'fes_vendors';
$data_array = array(
'requests' => $_POST['getval']
);
$data_where = array('requests' => $_POST['getval']);
$wpdb->update($table_name,$data_array,$data_where);
}
html
<form id="myForm" name="myform" action="" method="POST" style="padding:20px;">
<label> Select if this Vendor wishes to receive customer requests or not: </label>
<select name="getval" id="brandSel" size="1">
<option selected="selected" disbaled value="">-- Select status --</option>
<option value="1">Enable</option>
<option value="0">Disable</option>
</select>
<?php submit_button('submit'); ?>
</form>
我試了幾次,但數據沒有被保存/更新。 這是db的圖片
你的條件沒有意義。
假設用戶選擇了enable
-> $_POST['getval'] = 1;
您的查詢將是:
更新{table} SET requests = 1 WHERE requests = 1
我相信您應該將您的條件更新到特定行。 您可以通過在表單中添加一個隱藏輸入來實現,其中包含您希望更新的特定行。 並將您的條件更新為:
"id" => $_POST['row_id']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.