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;