[SOLVED] MYSQL Inserting New Users

14 years 5 months ago #116558 by PMGB2BSub
[SOLVED] MYSQL Inserting New Users was created by PMGB2BSub
Hi, I'm working on a component which will allow Site registration for CBSub Plans via a separate Front End Component.

(Companies who purchase a site license to our content would be able to register numerous users via a separate log-in page free of charge since it's already paid for)

I've worked out the primary registration Joomla insert process(query1, query2, and query3), however I'm looking to also add these users to the "jos_comprofiler" table as well as any other required table entries. Can someone help me out with this? Thanks

[code:1]
function Insert_User($fname, $lname, $email, $username, $password, $plan_id)
{
jimport('joomla.user.helper');
$name = $fname.$lname;
$params = "";
$ip_addr = $_SERVER;

$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password, $salt);
$password = $crypt.':'.$salt;

$date = date('Y-m-d H:i:«»s');
$query = "INSERT INTO jos_users
(name, usertype, username, email, password, registerDate, gid, sendEmail, block, params, lastvisitDate)
VALUES
('".$name."', 'Registered', '".$username."', '".$email."', '".$password."', '".$date."', '18', '0', '0', '".$params."', '".$date."')";


$query2 = "INSERT INTO jos_core_acl_aro (id, section_value, value, order_value, name, hidden)
VALUES (NULL, 'users', LAST_INSERT_ID(), '0', '".$name."', '0')";

$query3 = "INSERT INTO jos_core_acl_groups_aro_map VALUES (18, '', LAST_INSERT_ID())";

$query4 = "INSERT INTO jos_comprofiler
(id, user_id, firstname, lastname, hits, message_number_sent, avatar, avatarapproved, approved, confirmed, registeripaddr, banned)
VALUES (NULL, LAST_INSERT_ID(), '".$fname."', '".$lname."', '0', '0', NULL, '1', '1', '1', '".$ip_addr."', '0')";


$query = mysql_query($query);
$query2 = mysql_query($query2);
$query3 = mysql_query($query3);
$query4 = mysql_query($query4);
}
[/code:1]

Oh right, and $query4 is returning false for some reason. Thanks again for the assist.

Post edited by: Schweppesale, at: 2009/11/10 19:42

Post edited by: krileon, at: 2009/11/10 21:55

Please Log in to join the conversation.

14 years 5 months ago #116562 by krileon
Replied by krileon on topic Re:MYSQL Inserting New Users
Way too many queries, CB has API in place to add users like you're trying to do. First step, navigate to tutorials in my signature and read, slowly and carefully, Include CB API.

Once you've done this you can now use the following function. It has been modified to match your variables (should be a matter of copy/paste with a few changes of course).

[code:1]
function registerUser( $fname, $lname, $email, $username, $password, $plan_id ) {
global $_CB_framework, $_CB_database;

$row = new moscomprofilerUser( $_CB_database );
$row->usertype = $_CB_framework->getCfg( 'new_usertype' );
$row->gid = $_CB_framework->acl->get_group_id( $row->usertype, 'ARO' );
$row->confirmed = 1;
$row->approved = 1;
$row->block = 0;
$row->sendEmail = 0;
$row->registerDate = date( 'Y-m-d H:i:«»s', $_CB_framework->now() );
$row->first_name = $fname;
$row->last_name = $lname;
$row->username = $username;
$row->email = $email;
$row->password = cbHashPassword( $password );

if ( ! $row->store() ) {
trigger_error( 'Add User SQL error: ' . $row->getError(), E_USER_ERROR );
}
}
[/code:1]


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

14 years 5 months ago #116564 by PMGB2BSub
Replied by PMGB2BSub on topic Re:MYSQL Inserting New Users
modified(required file with cbhashpassword() was missing):
[code:1]
function registerUser( $fname, $lname, $email, $username, $password, $plan_id ) {
global $_CB_framework, $_CB_database;
jimport('joomla.user.helper');
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password, $salt);
$password = $crypt.':'.$salt;


$row = new moscomprofilerUser( $_CB_database );
$row->usertype = $_CB_framework->getCfg( 'new_usertype' );
$row->gid = $_CB_framework->acl->get_group_id( $row->usertype, 'ARO' );
$row->confirmed = 1;
$row->approved = 1;
$row->block = 0;
$row->sendEmail = 0;
$row->registerDate = date( 'Y-m-d H:i:«»s', $_CB_framework->now() );
$row->first_name = $fname;
$row->last_name = $lname;
$row->username = $username;
$row->email = $email;
$row->password = $password;

if ( ! $row->store() ) {
trigger_error( 'Add User SQL error: ' . $row->getError(), E_USER_ERROR );
}
}
[/code:1]

returns the following error msg:
Fatal error: Add User SQL error: Please enter your name

Am I missing something obvious? :blink:

Post edited by: Schweppesale, at: 2009/11/10 20:14

Please Log in to join the conversation.

14 years 5 months ago #116570 by krileon
Replied by krileon on topic Re:MYSQL Inserting New Users
[code:1]
function registerUser( $fname, $lname, $email, $username, $password, $plan_id ) {
global $_CB_framework, $_CB_database;

$row = new moscomprofilerUser( $_CB_database );
$row->usertype = $_CB_framework->getCfg( 'new_usertype' );
$row->gid = $_CB_framework->acl->get_group_id( $row->usertype, 'ARO' );
$row->confirmed = 1;
$row->approved = 1;
$row->block = 0;
$row->sendEmail = 0;
$row->registerDate = date( 'Y-m-d H:i:«»s', $_CB_framework->now() );
$row->name = $fname . ' ' . $lname;
$row->first_name = $fname;
$row->last_name = $lname;
$row->username = $username;
$row->email = $email;
$row->password = cbHashPassword( $password );

if ( ! $row->store() ) {
trigger_error( 'Add User SQL error: ' . $row->getError(), E_USER_ERROR );
}
}
[/code:1]

You're missing the name field; my mistake.

[code:1]
$row->name = $fname . ' ' . $lname;
[/code:1]

You need to use the cb function for generating passwords. Please read again how to include cb api. There's multiple imports you can do and you're likely missing:
[code:1]
import( 'cb.html' );
[/code:1]

Post edited by: krileon, at: 2009/11/10 20:42


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

14 years 5 months ago #116576 by PMGB2BSub
Replied by PMGB2BSub on topic Re:MYSQL Inserting New Users
Had to rename the following two lines:
[code:1]
$row->firstname = $fname;
$row->lastname = $lname;
[/code:1]

looks like it's working though, thanks man. B)

One last step, I need to be able to subscribe these users to a specific plan id within CBSubs.

Is this type of functionality provided within the API?

Post edited by: Schweppesale, at: 2009/11/10 20:59

Please Log in to join the conversation.

14 years 5 months ago #116583 by krileon
Replied by krileon on topic Re:MYSQL Inserting New Users
No CBSubs API is not yet provided; unfortunately you're on your own with that aspect. The best approach is probably just a query, which now you can use CB API to perform such query.

Example:
[code:1]
$query = 'DELETE'
. "\n FROM " . $_CB_database->NameQuote( '#__TABLE' )
. "\n WHERE " . $_CB_database->NameQuote( 'id' ) . " = " . $_CB_database->Quote( $id );
$_CB_database->setQuery( $query );
if ( ! $_CB_database->query() ) {
trigger_error( 'Delete SQL error: ' . $row->getError(), E_USER_ERROR );
}
[/code:1]


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.216 seconds

Facebook Twitter LinkedIn