Hi All,
I was wondering if there was a way that I could check if an ID already exists if not reserve it for the final query? Maybe with some sort of transaction based query.
So if I were to do this:
$exists = 1;
while($exists > 0) {
$new_id = uniqid();
$exists = $database->prepare('SELECT COUNT(*) FROM `Employees` WHERE `EmployeeID` = :EmployeeID');
$exists->execute([":EmployeeID" => $new_id]);
$exists = $exists->rowCount();
}
Along with:
$query = $database->prepare("
INSERT
INTO
`Employees`
(
`EmployeeID`,
`Username`,
`Password`,
`FirstName`,
`LastName`,
`Email`,
`Timestamp`,
`Roles_Role`
)
VALUES
(
:EmployeeID,
:Username,
:Password,
:FirstName,
:LastName,
:Email,
:Timestamp,
:Roles_Role
)
");
$query->execute([
":EmployeeID" => $new_id,
":Username" => $this->Username,
":Password" => $this->Password,
":FirstName" => $this->FirstName,
":LastName" => $this->LastName,
":Email" => $this->Email,
":Timestamp" => $this->Timestmap,
":Roles_Role" => $this->Roles_Role
]);
$query->execute();
I may get an error where if I check if the new custom ID exists, if it doesn't then the next query may fail to duplicates.
Thank you for any help in advance.