SOLVED: Update field value in unrelated table upon successful submission of form

boyjah

Member
I want to update the value of a single field in table comprofiler for the logged in user upon successful submission of my Fabrik form. I have spent days reading forum posts, studied the php plugin and other related documentation over and over and tried so many different things but I still cannot understand how to accomplish this seemingly simple task.

When I perform it manually in phpMyAdmin, this is the result:

UPDATE `y2vfp_comprofiler` SET `cb_usertype` = 'h' WHERE `y2vfp_comprofiler`.`id` = 539;

In this case "539" is the user id of the logged in user.

How can I turn that statement into something that the php plugin can process? Can I do this using the php plugin? Should I use the upsert plugin? Do I need a databasejoin? I have tried SO MANY things!
 
I understand how to get the userid and I have read Common PHP Tasks, but I can't make sense of it. Common php tasks needs more explanation for it to be useful. As is, I can't see how the example given is useful for the case I have described--am I joining something? In $IDinput='{rowid}'; going to do what I need? I have searched and searched. I would look on another website but I'm not even sure what search terms to use. Please can someone give a little more of a hint than
$user= JFactory::getUser();
$userid=$user->get('id');
...

It's the ... part that I need more clues for.
 
Well, that's why we have subscription support, for when you get out of your depth.

Are you needing to update that table with a value from the form, or is it always the 'h' given in your example?

-- hugh
 
Then you pretty much have everything you need. Troester gave you the two lines to get the logged on userid, and the 'update' wiki page gives you a good example of how to craft an update query.

Putting it together would look something like this:

Code:
// get the user ID
$user = JFactory::getUser();
$userid = $user->get('id');

// Get the db object
$myDb = JFactory::getDbo();

// Build the query
$myQuery = $myDb->getQuery(true);

$myQuery
     ->update('y2vfp_comprofiler')
     ->set('cb_usertype = "h"')
     ->where('id = '.(int)$userid);

$myDb->setQuery($myQuery);

// Run the query
$myDb->execute();

-- hugh
 
Damn it man! It works! You are my Hero and FABRIK is now an essential part of our tool kit. I have paid for a Professional Subscription. Thank you thank you thank you!!!

I was hesitating to take out yet another subscription for yet another component that over promised and under performed. I have already paid for ChronoForms/ChronoConnectivity and BreezingForms/Content Builder, only to find that paying simply means more frustration when questions go unanswered or are poorly answered ("There are plenty of examples on the internet--go find them!"), not to mention that they DON'T actually do what we are told in presales questions. In this case, Fabrik is the only database/CCK that we have found that provides true multilingual capability completely and correctly, with many options and approaches. The others failed when it came to translating values from the database when presenting in list/detail form. Fabrik does this and we are very grateful. Also, while it is not perfect, the documentation and tutorials available for Fabrik are far better than what most other components provide, in our experience.

So in hopes that it may help others with similar needs, here is my summary. My users do a basic registration and pay for membership using Community Builder, at which time they are put into a Pending user group in order to limit what they can do and see on the site until they have completed their profile form, which I made using Fabrik. Upon succesful completion of the profile form, their user group is changed from the Pending group to a full member group (I can provide that sample code if anybody is interested). At the same time, I also wanted to update a marker field in the Community Builder user database that indicated that their profile had been completed. Thus, in addition to saving their profile information from the Fabrik form in the data table for that form and sending off various emails to users and to admin, I also needed to update a field in the Community Builder data table (#__comprofiler). The field I needed to update is called "cb_usertype" and the value I wanted to insert into that field is the letter "h".

Conditions for the form that allow this code to work: the form is only accessible to registered, logged in users who are in the Pending user group.

The first 2 lines of the working code get the logged in user's user id, which is needed in order to update the field "cb_usertype" for the correct person. The next few lines about getting the db object and building the query are not intuitive to me, but it seems they are necessary for most database access functions in the php plugin. The key was the following lines:
->update('#__comprofiler')
->set('cb_usertype = "h"')
->where('id = '.(int)$userid);

This is the original code for my operation,

UPDATE `y2vfp_comprofiler` SET `cb_usertype` = 'h' WHERE `y2vfp_comprofiler`.`id` = 539;

which I got from manually changing a value in phpMyAdmin and copying the result (y2vfp is the table prefix in my particular Joomla setup. Yours will be different, which is why the code uses the standard #__ in front of the table name. Note that there are 2 underbars after the # symbol! Joomla knows to replace #__ with the prefix associated with your Joomla tables when processing code). the number 539 is the user id of the particular record that I manually modified. For the purpose of the form meant for all registered users, we must make this a variable that is picked up each time from the "get the user ID" step.

So this: UPDATE `y2vfp_comprofiler` SET `cb_usertype` = 'h' WHERE `y2vfp_comprofiler`.`id` = 539;
Became this: $myQuery->update('#__comprofiler')->set('cb_usertype = "h"')->where('id = '.(int)$userid);

The remaining lines about setting and running the query seem to be necessary to set all this into motion and make it do what has been instructed.

I put this code into the php plugin, under the Plug-ins tab for my form.
- - do - - = php
In = Both
On = New
Process script = End of form submission (onAfterProcess)
PHP File = - None Selected -
Require Once = No

And here is the PHP code

// get the user ID
$user = JFactory::getUser();
$userid = $user->get('id');
// Get the db object
$myDb = JFactory::getDbo();
// Build the query
$myQuery = $myDb->getQuery(true);
$myQuery
->update('#__comprofiler')
->set('cb_usertype = "h"')
->where('id = '.(int)$userid);
$myDb->setQuery($myQuery);
// Run the query
$myDb->execute();

I HOPE THIS IS HELPFUL!
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top