admin管理员组文章数量:1201859
In the WordPress table named wp_fes_vendors, there is a total of 12 columns.
Out of these 12 columns, I want to only update the data of column named requests
The data i am getting for this column is from a custom form in backend stored in $_POST['getval']
Here is the code i tried
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>
I tried several times but the data is not being saved /updated. Here is a picture of the db
In the WordPress table named wp_fes_vendors, there is a total of 12 columns.
Out of these 12 columns, I want to only update the data of column named requests
The data i am getting for this column is from a custom form in backend stored in $_POST['getval']
Here is the code i tried
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>
I tried several times but the data is not being saved /updated. Here is a picture of the db
Share Improve this question edited Apr 9, 2022 at 13:09 luke_mclachlan 1034 bronze badges asked Feb 14, 2020 at 8:03 NimeshNimesh 4413 gold badges7 silver badges19 bronze badges1 Answer
Reset to default 2You're using $wpdb->where()
incorrectly.
In your code, $data_array
and $data_where
are going to be exactly the same, so if we assume that $_POST['getval']
is 1
, the resulting query will be:
UPDATE wp_fes_vendors SET requests=1 WHERE requests=1
You see how that doesn't make sense? It's setting the requests
column to 1
for all rows where requests
is already 1
. It's a query that will never actually do anything.
The correct way to use $wpdb->where()
is to make sure that $data_array
contains the column names, and the new values that you want to set them to, while $data_where
should contain columns and values to use to identify which row should be updated.
For example, if I want to set requests
to 1
for the vendor with the ID of 5
, I need to do this:
$data_array = array(
'requests' => 1
);
$data_where = array(
'id' => 5
);
$wpdb->update( $table_name, $data_array, $data_where );
The result of that query will be:
UPDATE wp_fes_vendors SET requests=` WHERE id=5
So this means that in your form, you need to POST both the new value of the column, as well as the ID of the vendor that you need to update.
本文标签: plugin developmentUpdateinsert only a column of database table
版权声明:本文标题:plugin development - Updateinsert only a column of database table 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738636241a2104050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论