0

I want to have agentid generated randomly and unique to every agent. so i wrote this code but i get error message undefined variable $agentid. please, who can help me identify the problem, why my code is not generating agentid and none of my error checking is working. It keep inserting data into sql database without checking if it passes error checking. Please, where did i get it wrong

if(isset($_POST['membershiptype']) === "agent") {


        if (!isset ($_POST['refeeralid'])) {

            $_SESSION['error'] ='Please, add a refeeral Id. If you do not have any, use E1S2Z3';
            header("Location:register.php");
            return;

        } else {

            $refeeralidQuery = "SELECT * FROM agent WHERE agent_id = :agentid, LIMIT 1";
            $stmt = $pdo->prepare($refeeralidQuery);
            $stmt->execute( array (
                ':agentid' => $_POST['refeeralid']) );
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            

            if ( $row > 0) {
                

                

                function getRandomString($n) {
                $characters = '0123456789,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                $randomString = '';
      
                    for ($i = 0; $i < $n; $i++) {
                    $index = rand(0, strlen($characters) - 1);
                    $randomString .=$characters[$index];
                    }
      
                return $randomString;
    
                
    
                } 

                $n=7;
                $agentid = getRandomString($n);

                echo $agentid;
                
            } else {
                $_SESSION['error'] = 'Incorrect Refeeral ID. Please, check your entry. If you do not have refeeral id, please use E1S2Z3';
                }
        }
        
        
        

        
    }
        
    
    $token ='uu777fdft6';


        
        $sql = "INSERT INTO agent (agent_id, Surname, First_name, Other_names, Email, Password, Phone_number, Refeeral_id, verified, token)
        VALUES (:agentid, :surname, :firstname, :othernames, :email, :password, :phonenumber, :refeeralid, :verified, :token)";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array(
        ':agentid' => $agentid,
        ':surname' => $_POST['surname'], 
        ':firstname' => $_POST['firstname'],
        ':othernames' => $_POST['othername'],
        ':email' => $_POST['email'],
        ':password' => $_POST['password'],
        ':phonenumber' => $_POST['phonenumber'],
        ':refeeralid' => $_POST['refeeralid'],
        ':verified' => $_SESSION['verified'],
        ':token' => $token));

$_POST['membershiptype'] is a radio button.
Stephen
  • 15
  • 6

2 Answers2

1

Php has a function for generate unique id.

Uniqid();

https://www.php.net/manual/es/function.uniqid.php

But if you want to continue with your function the problem is that you declare $agentid inside the it and after the return, that cause the end of the process.

In resume you have to put the $agentid line out of the function not inside.

Joan
  • 26
  • 2
0

There is an error in your usage. You should use it as follows.

<?php 
function getRandomString($n) {
    $characters = '0123456789,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
      
    for ($i = 0; $i < $n; $i++) {
        $index = rand(0, strlen($characters) - 1);
        $randomString .=$characters[$index];
    }
    return $randomString;
    
}
$n=7;
$agentid = getRandomString($n);
echo $agentid;
?>