If you repeat, how can you go back to the previous step and continue?

$public_prod_id = "C".rand(1000000000,9999999999);
    $_SESSION["public_prod_id"] = $public_prod_id;

 $exist = mysqli_num_rows($pdo->query(
      "SELECT * FROM `product`
      WHERE `public_prod_id` = "{$_SESSION["public_prod_id"]}" "
    ));
if($exist == 0){
$pdo->query(
      "INSERT INTO ``
      (
        ``
      )
      VALUES
      (
        "{}"
      )
      "
    );
 }

if the result found is greater than 0
, how can I go back to it and generate a new set of codes and then walk again?

Mar.28,2021

while continue break;


encapsulate this operation as a function, call yourself again if the query result is greater than 0, and exit


until your conditions are met.
// 1. id
do{
    $public_prod_id = 'C'.rand(1000000000,9999999999);
    $_SESSION['public_prod_id'] = $public_prod_id;

     $exist = mysqli_num_rows($pdo->query(
          "SELECT * FROM `product`
          WHERE `public_prod_id` = '{$_SESSION['public_prod_id']}' "
    ));
}while($exist > 0);
// 2. 
INSERT INTO ...

there is a problem with the above method. If there is a new repetition between 1 and 2, although the probability is very low, the error will still be reported when inserting.
improvement:

do{
    $public_prod_id = 'C'.rand(1000000000,9999999999);
    $_SESSION['public_prod_id'] = $public_prod_id;

     $exist = mysqli_num_rows($pdo->query(
          "SELECT * FROM `product`
          WHERE `public_prod_id` = '{$_SESSION['public_prod_id']}' "
    ));
    if($exist == 0){
        try{
            Insert ...
        }catch(\Exception $e){
            $exists = 1;
        }
    }
}while($exist > 0);
In fact, there is another problem with this. As the amount of data increases, this function will be repeated more and more times.

  1. use an id pool. Remove the id, from the id pool every time you use it.
  2. adjust the generation strategy of id
Menu