MyBB Community Forums

Full Version: Force record in userfields table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have created a custom field and I want to populate it for a certain bunch of users and if not everyone.

I am using update to update the records in this table but obviously its not going to exist for some users, as such I need to do an insert with on duplicate

I currently use the following code to update inside my plugin

$sql = "UPDATE ".TABLE_PREFIX."userfields SET fid4 = CASE ufid ";
	foreach ($updatecase as $id => $ordinal) {
		$sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal);
	}
	$sql .= "END";

I am going to struggle to turn that into an insert with duplicate key check.

The only other option I have is that on signup, I can put the record in place.

Any thoughts?[/align]

Ok I got the query to work with an insert and then on duplicate key which works fine, and will suit my purposes, is there any other thoughts on this though
Are these users part of a specific group or part of something else specific where you can update "fid4" ?
Its part of a tipping competition I have written.

I wont include this part in the final release for the simple reason it was too much of a pain haha.

Even just getting the custom field ID was a pain without doing excessive queries in the postbit.

I will be looking at it further I found the best way was to write a query that just does

Insert (ufid, fid4) Values
(1,2),
(2,3)
ON DUPLICATE KEY UPDATE
field1=values(ufid),
field2=values(fid4)

Does the trick and is efficient enough as I just use an array to gather and populate the data.

it is also better than using sprint_f which is not meant to be used in mybb I believe from memory reading the coding guidelines. I might update my other queries that use sprint_f to do the same