Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End If ID does not exist? (PHP)

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36433
    schart
    Participant

    To be specific, I have an profile.php page that generates itself after the id gotten from the URL. Ex: profile.php?id=1 goes to the user “Schart” with the info[…]

    Now how do I determine if the ID in the url ($id) does exist in the database, and alternatively if it ever has?

    http://underbakke.net/profile.php?id=1

    #95832
    crgeary
    Member

    To determine if the ID exists in the database:

    Query the database for that ID
    Check to see how many records where returned, if no records are returned, then the ID does not exist. However if 1 record is returned, then the ID does exist.

    To check if it ever has been in there, i suppose you could go about it 2 ways..

    The first way, i guess you could just find out what the largest ID is, and if the ID is any lower than that a post must have existed..

    A better way would be to keep a record of all posts, even if you delete one, keep a record of it in the database somewhere and set the status of that post to deleted.

    #95908
    bungle
    Member

    In order to provide the latter functionality as crgeary says, when a user deletes their profile, you would really want to mark their user ids record as inactive rather than deleting it. I would add a boolean field of ‘active’ to the user table and then you can do a check like this to see what you are dealing with:

    
    $id  = mysql_real_escape_string($_GET);
    $sql = "SELECT * FROM users WHERE id=$id";
    $result = mysql_query($sql);

    if (mysql_num_rows($result)==0) {
    echo ('User never existed');
    }
    else if (mysql_num_rows($result)==1) {
    $userinfo = mysql_fetch_array($result);
    if (!$userinfo) {
    echo ('User no longer exists.');
    }
    else {
    echo ('User exists and is active.')
    }
    }
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.