'Custom Unique Id SQL and PHP [duplicate]
How would I go about seeing if order id is taken and if it is taken then to make another one and see if its unique in the database.
I've got this prototype down however no luck. Doesn't execute if there is nothing in the database. Also doesn't do anything if order id is not unique.
$order_id = md5(uniqid(mt_rand(), true));
foreach($conn->query('SELECT * FROM `bucks-orders`') as $item){
if ($item['order-id'] != $order_id){
$insertOrder = "<sql code here>";
$conn->query($insertOrder);
break;
}
}
Solution 1:[1]
If you have access to the DB, try setting the field to 'unique'.
This should do the checking for having one entry matching one id, never duplicating.
It does not sound like you after a mathematically unique number but just a unique id per DB field
Info link:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MisterG13 |
