treehouse : what would you like to learn today?
Web Design Web Development iOS Development

If ID does not exist? (PHP)

  • 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
  • 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.
  • Ok, but I am really new at PHP.

    <?php

    $query1 = mysql_query("SELECT id FROM husers");
    $aaa = "";
    while($rows = mysql_fetch_array($query1)){
    $aaa = $rows['id'];
    // Now what?
    }
    //Maybe something like this?:
    if (!$aaa){
    // 404 page here
    }


    ?>


    Could you maybe throw me a simple script?

    Also, the find ID thing is the !important one, that I really need
  • 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:
    <?php
    $id = mysql_real_escape_string($_GET['id']);
    $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['active']) {
    echo ('User no longer exists.');
    }
    else {
    echo ('User exists and is active.')
    }
    }
    ?>
  • Thank's man! :) Worked very well ;)
  • Also, since you guys seems to know a thing or two about this, would you mind helping me here too?
    http://css-tricks.com/forums/discussion/15885/nice-profile-urls#Item_1

    If you don't want to, don't bother ;)