Forums / Cotonti / Development / How to safe data from custom field in a user profile to the DB?

Macik
#41238 2015-12-15 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