Форуми / Cotonti / Development / How to safe data from custom field in a user profile to the DB?

foxhound
#1 14.12.2015 18:43

I am trying to create a new plugin which adds additional fields to a user profile.

I have gotten it so far that when I edit the user profile the fields are there and actually contain the data I insert to the DB upon plugin install. But I can for the life of me not figure out how to set the plugin up so that if the field value is changed it actually saves this new data when the user presses "submit".

I have created a php for the hooks to "do" when the user updates the profile:
Part=users.edit
File=subscriber.user.edit.update.done
Hooks=users.edit.update.done

I have been trying to get this to work since friday evening and I am about to throw myself out of the window.
Is there someone who can tell me what I need to do so that edited field values get saved to the DB?

 

Thanks in advance!


PS: yes I can use extra fields of course, but it would be nice if I can just make this a plugin so no additonal user input is needed when installing the plugin.

Added 4 hours later:

So here is the edit part of the plugin: 

if (!defined('SED_CODE')) { die('Wrong URL.'); }


$userid = sed_import('id','G','INT');

$sub_sql = sed_sql_query("SELECT user_subscr_end_date, user_subscr_date_stamp, user_subscriber_status FROM $db_users WHERE user_id='$userid'");
	if (sed_sql_numrows($sub_sql) > 0) {
		$sub = sed_sql_fetcharray($sub_sql);

		$sub_end = $sub['user_subscr_end_date'];
		$sub_stamp = $sub['user_subscr_date_stamp'];
		$sub_status = $sub['user_subscriber_status'];
		$sub_stamp = strtotime($sub_end);

	}

$t->assign(array(
	"USERS_EDIT_SUBSCRIPDATE" => "<input type=\"text\" class=\"text\" name=\"subscr_end_date\" value=\"".htmlspecialchars($sub['user_subscr_end_date'])."\" size=\"8\" maxlength=\"10\" />",
	"USERS_EDIT_SUBSCRIPSTATUS" => "<input type=\"text\" class=\"text\" name=\"subscriber_status\" value=\"".htmlspecialchars($sub['user_subscriber_status'])."\" size=\"1\" maxlength=\"1\" />"
));

 

And here is the update.done part of the plugin: 

defined('SED_CODE') or die('Wrong URL'); 

$userid = sed_import('id','G','INT');

$sub_sql_u2 = sed_sql_query("UPDATE $db_users SET user_subscr_end_date='".sed_sql_prep($sub_end)."', user_subscr_date_stamp='".sed_sql_prep($sub_stamp)."', user_subscriber_status='".sed_sql_prep($sub_status)."', user_auth='' WHERE user_id=$userid");

Now, I have tried about a few million different sql queries trying to perform the update and everything fails. My DB values are never updated when I change them in the user edit page.
If I change the update.done part of the plugin and (lets say) hardcode the DB values they get updated properly. So, the plugin works....I just can not figure out how to make the plugin update with the correct field values.

Added 8 hours later:

I tried a few dozen other different values for ".sed_sql_prep($sub_end)." to see if that would properly update the DB but all fial.

<img src="http://www.armaholic.com/datas/thumbs/green-sea-battalion-uniforms-version-03-preview_4.jpg" alt="green-sea-battalion-uniforms-version-03-" />

Відредаговано: foxhound (15.12.2015 08:03, 8 років тому)
Macik
#2 15.12.2015 18:09

First of all, lets figures out what Cotonti version you had use? is it Genoa?

If so, I'm tries to describe main concepts, but I'm not working with Genoa over 4 years. So my snippets will be correspond to Siena version. And it will be your homework to transform it to Genoa if needed. Fell free to ask more.

  1. If we need to store some user related data in connection with every user — the best way is to use extra fields. It automates a lot of work for view, edit, updating data in DB. Moreover (and it's important) if we use extra fields we can access our extra data everywhere we get user info, so no addition queries and hooks required.

    If you need to add extra field by code on install stage 
    - we loads `extrafields` package:
    require_once cot_incfile('extrafields');

    - use `cot_extrafield_add(…)` (see info here) to add field (you can define default value and more). Also do not foget to create uninstal part and use `cot_extrafield_remove(…)`.

    - Than you can use these extra fields as user ones. It's no matter will you use it in user templates or not. You can emulate user input, like this: 
    Hook `'users.edit.update.first'` and set `$_POST['user_youextrafield']` to desired value. Other work is done automatically — it would be updated in DB.
  2. If you still needs to interact with DB data you can use:
    - to get data from DB
    $sql = $db->query("SELECT user_subscr_end_date, user_subscr_date_stamp, user_subscriber_status FROM $db_users WHERE user_id= ?", $userid);
    $data = $sql->fetch();

    and
    $updated = $db->update($db_users, $data, "user_id=$userid");

    In terms of Genoa something like:
    $sql = sed_sql_query("SELECT user_subscr_end_date, user_subscr_date_stamp, user_subscriber_status FROM $db_users WHERE user_id='$userid'");
    $data = sed_sql_fetchassoc($sql);

    and
    sed_sql_update($db_users, $data, "user_id=$userid");

 

p.s. Without seeing error logs I can not guess why your queries working wrong. But you can enable debug mode (in config.php) and use `FOOTER_DEVMODE` tag in the footer.tpl to figure out whats going on.

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
foxhound
#3 16.12.2015 13:16

Wow, thank you very much Macik. That is really some very helpfull info :)
I can pretty much understand where I went wrong and I am going to apply some changes.

I should indeed just have gone for the extra fields, I was just trying to re-invent the wheel I guess :)


Yes, this is still a genoa website, still not gotten to the time to upgrade and figure out what some erros during the upgrade cause (I have tried but failed each time).
Once I have implemented some changes I will post here the results.
I actually plan to share this plugin as it might be usefull to others as well. With this plugin I add a subscription date I add to a users profile which adds the user to a specific group and when the date has passed he is automatically removed from the group. With this you can do all kind of fun stuff....at least I am working on some ;)

Added 1 hour later:

Sigh, I still cant get it to work.

I have created the fields through the extra fields for users. The fields are in my DB, if I change their value (manually directly in the DB) in my profile I can see the changed values.
I have added a file into my plugin for the "users.edit.update.first" hook. In it I added the $-post as you described yet still my DB is not updated when I update my profile with the new info.

I have enabled dbug mode, the "FOOTER_DEVMODE" tags were already present in my footer but I do not see any messages in my footer upon profile update. Does it only display errors if there are any or does it always display something (something in the sense of "dev info")?
So, I would like to share any erros with you, but there are none.

I must be overlooking something really simple, but I simply cant figure it out. :(

Added 2 hours later:

Got it!
Now that I use the user extra fields like you suggested I do not need this hook any more to insert the new field value as that is already done by just the user edit form itself. I was hookibg something which did not need that at all..
I am now only using the hook to get the new date stamp and insert that into the DB upon profile edit submit.

I am sure I will have some more questions but at least now that I can store dates I can work on the plugin funtionality itself :)

<img src="http://www.armaholic.com/datas/thumbs/green-sea-battalion-uniforms-version-03-preview_4.jpg" alt="green-sea-battalion-uniforms-version-03-" />

Відредаговано: foxhound (16.12.2015 17:17, 8 років тому)
Macik
#4 16.12.2015 19:50
#41239 foxhound:

I have added a file into my plugin for the "users.edit.update.first" hook. In it I added the $-post as you described yet still my DB is not updated when I update my profile with the new info.

I'm not shure that it should work in Genoa as Extra field utility not fully functional in that branch.

I have enabled dbug mode, the "FOOTER_DEVMODE" tags were already present in my footer but I do not see any messages in my footer upon profile update. Does it only display errors if there are any or does it always display something (something in the sense of "dev info")?

In common way `FOOTER_DEVMODE` should display full sql queries list with certain SQL code and it's timing. If you don't see it on any page check:
- `define('SED_DEBUG', TRUE);` in config.php
- enable debug mode in Admin panel site settings
- place FOOTER_DEVMODE tag in your footer.tpl
So you can find this dev info on all pages.
But !note: you can miss some SQL queries in case it done before actual redirect. This case is applied in User profile update too.

Another prehistoric way to debug is to add log line to your code to save info to logfile. You can save raw SQL query text than use some SQL client to run this query and seeing results.

But best way to deeper inspect you application — is to install some modern IDE and setup Debugger. Than you can see internals step-by-step.

So, I would like to share any erros with you, but there are none.

I must be overlooking something really simple, but I simply cant figure it out. :(

Added 2 hours later:

Got it!

Great.

I am sure I will have some more questions but at least now that I can store dates I can work on the plugin funtionality itself :)

You are welcome.

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F