db query_write doesn't write from plugins

I A 1

Enthusiast
Joined
Jun 7, 2015
Messages
138
There's a custom userfield which I am trying to update whenever a user updates his email address. The plugin is at hook location profile_updatepassword_complete.

This is the statement that I am using:
Code:
$userid = $vbulletin->userinfo['userid']; 
...
$vbulletin->db->query_write("
        UPDATE " . TABLE_PREFIX . "userfield
         SET field17 = 'Yes' WHERE userid = '".$userid."'
    ");

field17 is Radio buttons with 3 values: Yes, No, Fixed.

The plugin gets executed but when I check the value, this field has no data. It is simply blank.

But if I execute the same sql command using Execute SQL Query option in admincp, it works fine and 'Yes' option gets selected. Where am I making a mistake?

I am using vBulletin 4.

Edit:

I have previously also tried using the following with same results: field17 is blank with no value in it.
Code:
$field_17 = array('field17' => "Yes"); 
$userdata->set_userfields($field_17,false,'admin');
 

MarkFL

La Villa Strangiato
Joined
Jul 3, 2017
Messages
1,245
I would try using the query:

Code:
$userid = $vbulletin->userinfo['userid'];
...
$vbulletin->db->query_write("
        UPDATE " . TABLE_PREFIX . "userfield
        SET field17 = 'Yes'
        WHERE userid = " . $userid
);
 

Paul M

Super Moderator
Joined
Jun 26, 2006
Messages
4,077
There isnt anything wrong with the query.

I thinks its how you are trying to do it.
If you actually take a look at the hook in profile.php

Code:
($hook = vBulletinHook::fetch_hook('profile_updatepassword_complete')) ? eval($hook) : false;

// save the data
$userdata->save();

Notice its directly before the userdata save, I suspect that is immediately overwriting your change.
 

I A 1

Enthusiast
Joined
Jun 7, 2015
Messages
138
There isnt anything wrong with the query.

I thinks its how you are trying to do it.
If you actually take a look at the hook in profile.php

Code:
($hook = vBulletinHook::fetch_hook('profile_updatepassword_complete')) ? eval($hook) : false;

// save the data
$userdata->save();

Notice its directly before the userdata save, I suspect that is immediately overwriting your change.

How do you suggest this should be implemented?

I was actually trying to add an extra feature to the Bounced Email Handler by updating a custom profile field to Fixed if a user updates his email address. All users with bounced emails had their custom field set to No. This makes it easy to search for all users with invalid emails.
 

MarkFL

La Villa Strangiato
Joined
Jul 3, 2017
Messages
1,245
Try replacing your query with the following:

PHP:
$f17_value = 'Yes';
$userdata->set('field17', $value);
 

I A 1

Enthusiast
Joined
Jun 7, 2015
Messages
138
Try replacing your query with the following:

PHP:
$f17_value = 'Yes';
$userdata->set('field17', $value);

I tried this but it gives an error.

Fatal User Error: Field field17 is not defined in $validfields in class vB_DataManager_User in ..../includes/class_dm.php on line 515
 
Top