1

I 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 name requests

The data i am getting for this column is from a custom form in backend stored in $_POST['getval']

Here is 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 picture of db

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.