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.