lundi 5 avril 2021

check if value exists in database do return in PHP

I am trying to generate the unique transaction id with the help of my database. I inserting the data and generating the new random code then, fetching that random code from the database. if that random code exists return and generates again new random code until the new fresh unique random code I did not get. then the insert function will execute in PHP

I am doing this with the transaction function in PDO PHP

$con->beginTransaction();

$randomeCode = rand(10,100);

$stmt = $con->prepare("INSERT INTO `table1` (`message`) VALUES ('Hello_World')");
$stmt->execute();
$checkDone = $con->lastInsertId();
if ($checkDone) {
   $stmt = $con->prepare("SELECT `ID`,`column` FROM `table1` WHERE `column` = $randomeCode");
   $stmt->execute();
   if ($stmt->rowCount() > 0) {
      $con->rollback();
      $randomeCode = rand(10,100); //Generate new code and again check
      return; // again genearte the randome code and repeat the same processor 
   } else {
      $con->commit();
      //INSERT EXECUTE
   }
}



Aucun commentaire:

Enregistrer un commentaire