Appending numbers to unique database entries [MySQL]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
plezops
Forum Newbie
Posts: 11
Joined: Mon May 28, 2007 5:22 pm
Location: Austin, Texas

Appending numbers to unique database entries [MySQL]

Post by plezops »

Using a MySQL database I am adding file names and extensions into the database. There is a problem I am having though. The file names column is set to unique but I would like to use a php script to check if the file name is already there and if so append the next greatest number onto the end so that it will not return an error because a file name is already there. The next time the file was uploaded it would check for the file name, and upon finding both 'test' and 'test1' it would append a 2 to the file name making a 'test2'.

I am just starting with MySQL/PHP together so I don't know very many commands so any in depth explanations would be extremely useful.

Thanks
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I've done that before. It's exactly what your saying..
Just check the database then, if it exists, change the name and check the database for that name, and continue looping through until you have created a name that's safe to insert.
Post Reply