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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

You'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