How to get next index

Saw this in an earlier post by deano(no s):

That would be inaccurate. You would need to obtain the next index value. If the last member that was added was deleted then the next member id would not match the index thus would not be the actual member id that gets assigned to the new account.

You need to go by the index.

 

How would I get the next index number? My current code uses a random number generator which leaves room for the number to duplicated. Only a clinical provider can add a new Client/Patient member and below is my code for that:

function getBlockCode_AddClient()
    {
$aProfiles = $GLOBALS['MySQL']->getPairs("SELECT `ID` , `NickName` FROM `Profiles` WHERE UserType = 'Staff' OR UserType = 'Lead Staff'", 'ID', 'NickName');
    $userID = mt_rand(123123123, 999999999);
$aForm = array(
            'form_attrs' => array(
                'name'     => 'form_add_client',
                'method'   => 'post',
            ),

            'params' => array (
              
                      'db' => array(
                    'table' => 'Profiles', // table name
                    'key' => 'ID', // key field name
                    'uri' => '', // uri field name
                    'uri_title' => '', // title field to generate uri from
                    'submit_name' => 'submit_add_client_form', // some filed name with non empty value to determine if the for was submitted,
                                                       // in most cases it is submit button name
                ),
                'csrf' => array(
                      'disable' => true, //if it wasn't set or has some other value then CSRF checking is enabled for current form, take a look at sys_security_form_token_enable to disable CSRF checking completely.
                )
              ),
 'inputs' => array (
        'header1' => array(
            'type' => 'block_header',
            'caption' => _t("Basic Info"),
        ),
        'ID' => array(
            'type' => 'hidden',
            'name' => 'ID',
            'value' => $userID,
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'NickName' => array(
            'type' => 'text',
            'name' => 'NickName',
            'value' => $userID,
            'info' => 'This number is given to client as username to log in. The password is Password. Please advise them to change their password at their earliest convenience.',
            'attrs' => array (
            'readonly' => true,
            ),
            'caption' => _t("Client ID"),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'FirstName' => array(
            'type' => 'text',
            'name' => 'FirstName',
            'caption' => _t("First Name"),
            'attrs' => array (
                'autocomplete' => 'off',
            ),
            'checker' => array (
                'func' => 'length',
                'params' => array(1, 35),
                'error' => _t('_sys_adm_form_err_required_field'),
            ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'LastName' => array(
            'type' => 'text',
            'name' => 'LastName',
            'caption' => _t("Last Name"),
            'value' => '',
            'checker' => array (
                'func' => 'length',
                'params' => array(1, 35),
                'error' => _t('_sys_adm_form_err_required_field'),
            ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Sex' => array(
            'type' => 'radio_set',
            'name' => 'Sex',
            'caption' => _t("Gender"),
            'value' => '',         
            'values' => array (
                'Male' => 'Male',
                'Female' => 'Female',
            ),           
            'checker' => array (
                'func' => 'avail',
                'error' => _t('_sys_adm_form_err_required_field'),
            ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'DateOfBirth' => array(
            'type' => 'date',
            'name' => 'DateOfBirth',
            'caption' => _t("Date Of Birth"),
            'value' => '',
            'checker' => array (
                'func' => 'length',
                'params' => array(1, 12),
                'error' => _t('_sys_adm_form_err_required_field'),
            ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Phone' => array(
            'type' => 'text',
            'name' => 'Phone',
            'value' => '',
            'caption' => _t("Phone"),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Email' => array(
            'type' => 'text',
            'name' => 'Email',
            'value' => '',
            'caption' => _t("Email"),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Comments' => array(
            'type' => 'textarea',
            'name' => 'Comments',
            'value' => '',
            'caption' => _t("Comments"),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Provider' => array(
            'type' => 'select',
            'name' => 'Provider',
            'value' => '',
            'caption' => _t("Provider"),
                       'values' => $aProfiles,
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Service' => array(
            'type' => 'hidden',
            'name' => 'Service',
            'value' => '',
            'caption' => _t("Service Type"),
                      'values' => array (
                        'Individual' => 'Individual', 
                        'Couple' => 'Couple',
                        'Group' => 'Group',
                    ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'ClientStatus' => array(
            'type' => 'hidden',
            'name' => 'ClientStatus',
            'value' => '',
            'caption' => _t("Status"),
            'values' => array ( 
                        'Pending Assignment' => 'Pending Assignment', 
                        'Pending Triage Evaluation' => 'Pending Triage Evaluation', 
                        'Pending Nursing Evaluation' => 'Pending Nursing Evaluation',
                        'Inpatient (Active)' => 'Inpatient (Active)',
                        'IOP Patient (Active)' => 'IOP Patient (Active)',  
                        'PHP Patient (Active)' => 'PHP Patient (Active)', 
                        'Awaiting Consult' => 'Awaiting Consult', 
                        'Consult followup' => 'Consult followup', 
                        'Inactive' => 'Inactive', 
                    ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
                ),
        'Password' => array(
            'type' => 'hidden',
            'name' => 'Password',
            'caption' => 'Password',
            'value' => '697e48bf9a0ee12ea897142800f2759d46ed0512',
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'Salt' => array(
            'type' => 'hidden',
            'name' => 'Salt',
            'caption' => 'Salt',
            'value' => 'yykEY5Gv',
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
                'header1_end' => array(
                    'type' => 'block_end'
                ),
        'header2' => array(
            'type' => 'block_header',
            'caption' => _t("Referral Info"),
        ),
        'Referral' => array(
            'type' => 'radio_set',
            'name' => 'Referral',
            'caption' => _t("Referred"),
                      'values' => array (
                        'Yes' => 'Yes', 
                        'No' => 'No',
                    ),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'ReferralSource' => array(
            'type' => 'text',
            'name' => 'ReferralSource',
            'value' => '',
            'caption' => _t("Referral Source (Name)"),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
        ),
        'ReferralReason' => array(
            'type' => 'textarea',
            'name' => 'ReferralReason',
            'value' => '',
            'caption' => _t("Referral Notes"),                  
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
                ),
        'InsuranceIssues' => array(
            'type' => 'text',
            'name' => 'InsuranceIssues',
            'value' => '',
            'caption' => _t("Insurance Issues"),
                      'db' => array (
                        'pass' => 'Xss', 
                    ),
                ),
                'header2_end' => array(
                    'type' => 'block_end'
                ),
        'submit_send' => array(
            'type' => 'submit',
            'name' => 'submit_add_client_form',
            'value' => _t("Add client"),
        ),
    )
);




 $oForm = new BxTemplFormView ($aForm);
        $oForm->initChecker();

        if ($oForm->isSubmittedAndValid ()) {
            // add additional vars to database, in this case creation date field is added
            $aValsAdd = array (
            'status' => 'Active',
            );
if ($oForm->insert ($aValsAdd)) {
   $NickName = $_POST['NickName'];
   $FirstName = $_POST['FirstName'];
   $LastName = $_POST['LastName'];
   $time = time();
   $When = date("Y-m-d h:i:s", $time);
   $Provider = $_POST['Provider'];
  
   $NewClient = mysql_query("INSERT INTO sys_client_list (`ID`, `Profile`, `Check`, `When`, `Services`) VALUES ('$Provider', '$NickName', '1', '$When', 'Individual')");

if ($NewClient) {
$me = $_COOKIE['memberID'];
header("location: viewClients.php?iUser=$me");
}

  $wall = mysql_query("INSERT INTO bx_wall_events (owner_id, object_id, type, content, title, description, date) VALUES ('$NickName', '$Provider', 'wall_common_text', '<div class=\"wall-post-text bx-def-font-h2\">New Client Added: $FirstName $LastName</div>', 'New Client Added: $FirstName $LastName', 'New Client Added: $FirstName $LastName', '$time')");
  

            }
        }
              
        return array($sStatusText . $oForm->getCode(), array(), array(), false);
       
     }
    
Any help would be greatly appreciated. Thanks in advance.

caredesign.net
Quote · 25 Sep 2013

OK, something else I jsut thought of, How would i be able to use my existing code, but make sure that the ID number is not duplicated. What would be the best way to do that. I am thinking of a security issue if I go with the index number, cause that number would be sequential. So, say I am added and told my Password is Password, what is to stop someone from adding a number and using the default password, and that person had not changed their password yet - this would present a problem. So, I would love any suggestions on which would be best to use and with the least amount of security issues.

caredesign.net
Quote · 25 Sep 2013

Getting the id would be done like this.

$result = mysql_query("SHOW TABLE STATUS LIKE 'Profiles'");
$rows = mysql_fetch_assoc($result);
$nextid =$rows['auto_increment'];

But that has a risk as well. If for example two accounts are being created at the same time, if the first one gets in before yours, then the number will still be inaccurate. The best method is to use mysql_insert_id() to get the id of the last row that was inserted. But that requires you create the account first, then show the information. But that will not work for what your doing.

So a good unique number would be better. But with small numbers that is hard to do. You could use the current unix time stamp but even that runs the risk of being duplicated if you create two accounts with 1 second of each other.

Is there a limit to the number of digits you would allow for a client/patient ID? And if alpha chars are allowed. A hash of a php microtime result would provide a pretty unique ID, bit it would also be huge.

There is also php's uniqid() function which generates a ID like this one. 4b3403665fea6 which is 13 characters long, or if the optional more_entropy is set to true, a ID that is 23 characters long.

Or, you store number like one in the database somewhere, or in a file. Then pull that value when you need a new id, add a random number to it from like 1 to 20 and use that as the ID and store that to use as the next starting point for the next number. In other words, you store your own index to go by.


https://www.deanbassett.com
Quote · 25 Sep 2013

Is there a limit to the number of digits you would allow for a client/patient ID? And if alpha chars are allowed.:

No there is no limit. But I am trying to make it simple for a Client to be able to remember it, as this is their UserName as well as their ID that will go with every piece of information for them. Medical Notes and Reports, and a whole slew of other things.

The possibility of two new Clients being added at the same time - not much of an issue, as the person allowed to enter new clients can only do one at a time.

But basically, it seems my easiest solution is to possibility keep the random number generation, but check to see if that number is already being used. If so, create a new random number.

caredesign.net
Quote · 25 Sep 2013
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.