How to give a unique number to add new product on website
-
30-05-2021 - |
سؤال
I am creating a ecommerce website, there i want to give a unique number to the product while adding. I want to do it in a way that it will automatically takes the number for this right now i m using time() function. with this a product get the time value while adding. But i dont know that with time function every time it will give a unique number or it can be repeat also if any body added product in a same time.
if any body have any other suggestion to give unique number or id to it pls tell me.
a reply will be appreciated.
المحلول
Using time() as mentioned will give you a sortable way to create unique IDs. Concatenating strings will also further randomize your desired result and still keep it sortable:
$uniqueId= time().mt_rand();
نصائح أخرى
Don't bother using time()
to uniquely name your products. Just use an MySQL auto_increment column and then if you need to retrieve the new ID from the database use PHP's mysql_insert_id()
function (or the equivalent for whatever database interface you're using).
create a table with the following schema
create table products
(
id int auto_increment primary key,
product nvarchar(40),
type nvarchar(10),
time timestamp default now(),
...
);
to insert data into the table use
insert into products(product,type)
values ('JEdit','Editor'),('BlueFish','Compiler');
Your table would look like this
id product type time ...
_____________________________________________________
1 JEdit Editor 2012-4-11 10:00:00
2 BlueFish Compiler 2012-4-11 11:00:00