How to update data in wordpress database table with functions.php?
To update data in your student table , use the update method of the $wpdb object, passing an array of field names and values to be updated, as well as an array of field names and values to use to find the record to update.
In WordPress, you can update data in a custom database table using $wpdb->update().
This works almost the same way as $wpdb->insert(), but includes a WHERE condition to target the correct row.
/* =================== DB: update phone (on demand) =================== */
function adc_update_student_phone() {
global $wpdb;
$table_name = $wpdb->prefix . “students”;
$wpdb->update(
$table_name,
array( 'phone' => '9999999999' ), // new data
array( 'roll_number' => '103' ), // where (string in schema)
array( '%s' ), // format for new data
array( '%s' ) // format for where (string not int!)
);
}
/**
Admin trigger: visit with ?adc_update_phone=1 while logged-in admin.
*/
function adc_update_trigger() {
if ( function_exists(‘adc_is_rest_request’) && adc_is_rest_request() ) return;
if ( ! is_user_logged_in() || ! current_user_can(‘manage_options’) ) return; if ( isset($_GET[‘adc_update_phone’]) && $_GET[‘adc_update_phone’] === ‘1’ ) {
adc_update_student_phone();
echo “✅ Student phone updated.”; // add confirmation
}
}
add_action(‘init’, ‘adc_update_trigger’);