-1

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.

Harish Kumar
  • 927
  • 3
  • 20
  • 46

3 Answers3

3

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).

arrowd
  • 33,231
  • 8
  • 79
  • 110
Camden S.
  • 2,185
  • 1
  • 22
  • 27
  • ys i can use unique_id also but with the numbers i want to give the product code also which will be prefix on the code while selecting category. like if i select the any product category from option list that category code will be prefix on that unique number. so the number will be something like this PKT33456762 – Harish Kumar Apr 11 '12 at 04:09
  • Where "PKT" is your category-specific prefix and "33456762" is a unique auto-incrementing number? – Camden S. Apr 11 '12 at 04:11
  • its specific in the database whenever i select the category from a option list it will prefix to the number generated by time() function – Harish Kumar Apr 11 '12 at 04:15
0

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 
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
0

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();
GoSmash
  • 1,096
  • 1
  • 11
  • 38