[SOLVED] Having trouble with external registration.

Colbydude

New member
Hey all, I'm having a bit of trouble trying to register a user by external means.

I'm currently using the class posted in this thread: http://xenforo.com/community/threads/how-to-create-a-bridge.28515/#post-332676, trying to use the createUser function.

Exact function:
PHP:
public static function createUser($sUsername, $sEmail, $sPassword, array $aAdditionalData = array())
{
    // Create the username from the person's name.
    $sUsername = str_replace(' ', "_", $sUsername);

    // Set User Data.
    $cWriter = XenForo_DataWriter::create('XenForo_DataWriter_User');
    $cWriter->set('username', $sUsername);
    $cWriter->set('email', $sEmail);
    $cWriter->setPassword($sPassword);
    $cWriter->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
    $cWriter->set('user_state', 'email_confirm');
    foreach ($aAdditionalData AS $data => $key)
    {
        $cWriter->set($data, $key);
    }
    $cWriter->save();
    $cUser = $cWriter->getMergedData();

    // Login new user: Log the ip of the user registering.
    XenForo_Model_Ip::log($cUser['user_id'], 'user', $cUser['user_id'], 'register');
    // Set the user back to the browser session.
    XenForo_Application::get('session')->changeUserId($cUser['user_id']);
    XenForo_Visitor::setup($cUser['user_id']);

    return $cUser['user_id'];
}

I'm currently running it using:
PHP:
// Create the XenForo User.
$user_id = $xenApp->createUser($_POST["USERNAME"], $_POST["EMAIL"], $_POST["PASSWORD"], array("dob_day"=>$_POST["DAY"], "dob_month"=>$_POST["MONTH"], "dob_year"=>$_POST["YEAR"]));

Just a simple call using some values after a form has been posted, for testing purposes.

However I'm getting the following error:
An exception occurred: Mysqli statement execute error : Field 'user_id' doesn't have a default value in /home/<removed>/public_html/forum/library/Zend/Db/Statement/Mysqli.php on line 214

  1. Zend_Db_Statement_Mysqli->_execute() in /home/<removed>/public_html/forum/library/Zend/Db/Statement.php at line 317
  2. Zend_Db_Statement->execute() in /home/<removed>/public_html/forum/library/Zend/Db/Adapter/Abstract.php at line 479
  3. Zend_Db_Adapter_Abstract->query() in /home/<removed>/public_html/forum/library/Zend/Db/Adapter/Abstract.phpat line 574
  4. Zend_Db_Adapter_Abstract->insert() in /home/<removed>/public_html/forum/library/XenForo/DataWriter.php at line1612
  5. XenForo_DataWriter->_insert() in /home/<removed>/public_html/forum/library/XenForo/DataWriter.php at line 1601
  6. XenForo_DataWriter->_save() in /home/<removed>/public_html/forum/library/XenForo/DataWriter.php at line 1393
  7. XenForo_DataWriter->save() in /home/<removed>/public_html/authoring/includes/xenforo_app.php at line 190
  8. appXenForo::createUser() in /home/<removed>/public_html/authoring/minecraft/pages/register.php at line 69
  9. include() in /home/<removed>/public_html/authoring/minecraft/index.php at line 43

What I find to be a little confusing is that it's saying that "user_id" doesn't have a default value. But isn't this supposed to be creating a new "user_id" for the created user? Or am I not understanding this correctly?

Any information on this would be great, thanks.
 
Yes, it is supposed to, yes that is very confusing!

Does the same error occur when trying to register inside xenForo?

Liam
 
Ah yes, looks like it does. I haven't noticed that before.

Here's the stack trace for error from within XenForo:
#0 /home/<removed>/public_html/forum/library/Zend/Db/Statement.php(297): Zend_Db_Statement_Mysqli->_execute(Array)
#1 /home/<removed>/public_html/forum/library/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 /home/<removed>/public_html/forum/library/Zend/Db/Adapter/Abstract.php(574): Zend_Db_Adapter_Abstract->query('INSERT INTO `xf...', Array)
#3 /home/<removed>/public_html/forum/library/XenForo/DataWriter.php(1612): Zend_Db_Adapter_Abstract->insert('xf_user', Array)
#4 /home/<removed>/public_html/forum/library/XenForo/DataWriter.php(1601): XenForo_DataWriter->_insert()
#5 /home/<removed>/public_html/forum/library/XenForo/DataWriter.php(1393): XenForo_DataWriter->_save()
#6 /home/<removed>/public_html/forum/library/XenForo/ControllerPublic/Register.php(294): XenForo_DataWriter->save()
#7 /home/<removed>/public_html/forum/library/XenForo/FrontController.php(347): XenForo_ControllerPublic_Register->actionRegister()
#8 /home/<removed>/public_html/forum/library/XenForo/FrontController.php(134): XenForo_FrontController->dispatch(Object(XenForo_RouteMatch))
#9 /home/<removed>/public_html/forum/index.php(13): XenForo_FrontController->run()
#10 {main}
 
Ah yes, looks like it does. I haven't noticed that before.

Here's the stack trace for error from within XenForo:

Very strange...

Are there any addons installed to xenForo overriding/extending the user datawriter?

Liam
 
No there's no addons being used at all in the forum.

However I did manually import users from our old system using a custom script before I had discovered the class in the first post.

Here's the script:
PHP:
<?php
    //LAST RUN DATE: 01/11/2014, LAST ID: 1157
    $conn = new PDO("mysql:host=localhost;dbname=<removed>",'<removed>','<removed>');
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
    $conn->exec("SET NAMES utf8");
  
    function convertToUtf8($string, $entities = null)
    {
        if(preg_match('/[\x80-\xff]/', $string))
        {
            if(function_exists('mb_convert_encoding')){ $string = mb_convert_encoding($string, 'utf-8', $this->_charset); }
            else if(function_exists('iconv')){ $string = @iconv($this->_charset, 'utf-8//IGNORE', $string); }
        }

        //$string = utf8_unhtml($string, $entities);
        $string = preg_replace('/[\xF0-\xF7].../', '', $string);
        $string = preg_replace('/[\xF8-\xFB]..../', '', $string);
        return $string;
    }
  
    $bunchofarrays = array(); $extradata = array();
  
    $query = $conn->query("SELECT * FROM users");
  
    while($row = $query->fetch(PDO::FETCH_ASSOC)){
        $bunchofarrays[] = array(
            'id' => $row['ID'],
            'username' => convertToUtf8($row['USERNAME']),
            'email' => convertToUtf8($row['EMAIL']),
            'gender' => strtolower($row['GENDER']),
            'register_date' => strtotime($row['DATE_REGISTERED']),
            'last_activity' => strtotime($row['LAST_DATE']),
            'user_group_id' => '2',
            'user_state' => 'valid'
        );
              
        $parts = explode('-', $row['DOB']);
        if(count($parts)==3){
            $dob_day = trim($parts[2]);
            $dob_month = trim($parts[1]);
            $dob_year = trim($parts[0]);
        }
      
        $extradata[] = array(
            'id' => $row['ID'],
            'location' => convertToUtf8($row['COUNTRY']),
            'dob_day' => $dob_day,
            'dob_month' => $dob_month,
            'dob_year' => $dob_year
        );
    }
  
    $conn = null;

    $conn = new PDO("mysql:host=localhost;dbname=<removed>",'<removed>','<removed>');
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
    $conn->exec("SET NAMES utf8");
  
    echo '<h2>Core data</h2>';
  
    foreach($bunchofarrays as $array){
        $array = array_values($array);
        echo '<p>Starting to port '.$array[0].'<br />';
        $query = $conn->prepare("INSERT INTO xf_user(`user_id`,`username`,`email`,`gender`,`register_date`,`last_activity`,`user_group_id`,`user_state`) VALUES(?,?,?,?,?,?,?,?)");
        echo "INSERT INTO xf_user(`user_id`,`username`,`email`,`gender`,`register_date`,`last_activity`,`user_group_id`,`user_state`) VALUES(".$array[0].",".$array[1].",".$array[2].",".$array[3].",".$array[4].",".$array[5].",".$array[6].",".$array[7].")".'<br />';
            $query->execute($array);
        $query = $conn->prepare("INSERT INTO xf_user_authenticate(`user_id`,`scheme_class`) VALUES(?,'XenForo_Authentication_Core12')");
            $query->execute(array($array[0]));
        $query = $conn->prepare("INSERT INTO xf_user_group_relation(`user_id`,`user_group_id`,`is_primary`) VALUES(?,?,?)");
            $query->execute(array($array[0],2,1));
        $query = $conn->prepare("INSERT INTO xf_user_option(`user_id`,`show_dob_year`,`show_dob_date`,`content_show_signature`,`receive_admin_email`,`email_on_conversation`,`is_discouraged`,`enable_rte`,`enable_flash_uploader`) VALUES(?,0,1,1,1,1,0,1,1)");
            $query->execute(array($array[0]));
        $query = $conn->prepare("INSERT INTO xf_user_privacy(`user_id`,`allow_view_profile`,`allow_post_profile`,`allow_send_personal_conversation`,`allow_view_identities`,`allow_receive_news_feed`) VALUES(?,'everyone','everyone','everyone','everyone','everyone')");
            $query->execute(array($array[0]));
        echo 'Done.</p>';
    }
  
    echo '<h2>Additional data</h2>';
  
    foreach($extradata as $array){
        $array = array_values($array);
        echo '<p>Starting to port '.$array[0].'<br />';
        $query = $conn->prepare("INSERT INTO xf_user_profile(`user_id`,`location`,`dob_day`,`dob_month`,`dob_year`) VALUES(?,?,?,?,?)");
            $query->execute($array);
        echo 'Done.</p>';
    }

    echo 'All done!';
?>

Then the user would just have to reset their password in order to log in. Looking at this now, I'd imagine this has something to do with it since I had IDs that already existed and input them into the XF database.
 
I think you may have fuddled up the internal auto_incremement counter...

Try truncating the table, and go again.

Liam
 
Ah, that was it! Not sure how the auto-increment got turned off. I must have done it before I ran the script and forgot about it.

Thanks for the help!
 
Back
Top Bottom