Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Don't Add Duplicate Time for Post

    I created a blog with an admin section and my friend wants to be able to change the published date/time of a post (it had formerly just added the time depending on when he published it).

    This is fine, but I NEED the time to be distinct in the database. How can I run a query that checks if the time I'm about to put in the DB is already in there and, if it is, adds 1 second to the time until I get a distinct time?

    Thanks for any help!


    Can it be as simple as this:

    Code:
    <?php
    while(!isset($time_success)){
    			$query = "SELECT * FROM posts WHERE time = '$time'";
    			$result = mysql_query($query);
    			if(mysql_num_rows($result)){
    				$time = $time + 1;
    			} else {
    				$time_success = 'success';
    			}
    		}
    ?>
    Last edited by mlmorg; 02-21-2009 at 01:04 AM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The strategy you included will work fine, maybe a little tweaking on incrementing the $time variable, but generally pseudo-codally that works.

    I would probably make sure my publish datetime value is an actual MySQL DateTime data type, which includes microseconds, so the possibility of a duplicate happening with that kind of precision is extremely remote. If the admin only enters date, hour and minute, you can randomize the seconds and microseconds to bring the chances you'll have a duplicate down to around .0000000001%.

    And of course, you'll eventually see a duplicate so just make sure the datetime has a unique index on it so the query will fail if it is a duplicate.

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok cool thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •