There is a function, the problem is that with an INSERT if a unique field is encountered, it gets an error after several updates occur. The field must have from 8-12 DIGITAL. Problem FLOOR(RAND() * 4010000000) + 1000000000 Tried UUID_SHORT as well:

 function bookingCreate($hotelid, $roomid, $dtarrive, $dtdepart, $price, $email) { $resultId = -1; $cn = dbConnect(); $timearrive = strtotime($dtarrive); $timedepart = strtotime($dtdepart); $newformatarrive = date('Ym-d',$timearrive); $newformatdepart = date('Ym-d',$timedepart); mysqli_query($cn,"INSERT INTO table(hotelid, roomid, dtarrive, dtdepart, userid, bookingcode, price, status, email) VALUES (".$hotelid.",".$roomid.",'".$newformatarrive."', '".$newformatdepart."',0, FLOOR(RAND() * 4010000000) + 1000000000, ".$price.", 1, '".$email."') ON DUPLICATE KEY UPDATE bookingcode=FLOOR(RAND() * 4010000000) + 1000000000"); $resultId = mysqli_insert_id($cn); mysqli_close($cn); return $resultId; 
  • Don't you think that html and smarty tags are superfluous in this question? - teran
  • And why do you need a whistle with a random house? Why not just add an incremental field? - ArchDemon
  • Because it is necessary that the sequence could not be traced in an easy way. - Den
  • Give your problem to the trigger BEFORE INSERT. He will do better. it is necessary that the sequence could not be traced easily by Joe because it is elusive because they do not catch him, because he doesn't need fucking anyone ... - Akina
  • And why do you need unpredictable values ​​in the database? Store the usual auto-increment id in the database. And when communicating with a client, use their AES encrypted views. - Mike

2 answers 2

No need to reinvent the wheel, everything has already been invented before us: http://php.net/manual/ru/function.uniqid.php

  • Friend you are right on time! Thank you, but Joe has chosen his thorny path! - Den

The solution in which the replay is almost impossible:

 $orderIdUnique = rand(1,99)."".(time()-strtotime("2016-01-01 00:00:00"))."".rand(1,99);