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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    Unknown column 'id' in 'where clause'?

    I'm trying to display the user's name by I keep getting this error: Unknown column 'id' in 'where clause'

    Code:
    <?php
    session_start();
    include('database.php');
    include('/home/xtraz/public_html/test/functions/index.php');
    if ($_SESSION['loggedin'] = 1) {
    echo "Welcome, ";
    find_name();
    }
    ?>
    Code:
    <?php
    session_start();
    include('/home/xtraz/public_html/test/database.php');
    function find_name()
    {
    $sql = mysql_query("SELECT first_name FROM `users` WHERE id='".$_SESSION['userid']."'") or die(mysql_error());
    $result = $sql;
    $row = mysql_fetch_array($result);
    echo $row['first_name'];
    }
    ?>
    Code:
    <?php
    if ($_POST['email'] == "" || $_POST['password'] == "") {
        print <<<EOF
    EOF;
        
    print "Error #101
    <p>Please make sure your information was entered correctly and try again.</p>";
    }
    require "/home/xtraz/public_html/test/register/database.php";
    global $c;
    global $ui;
    $check_users = mysql_query("SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'");
    if (mysql_num_rows($check_users) == 0) {
        print "Error #102 <p>The username or password you entered was incorrect.</p>";
    } else {
        $_SESSION['loggedin'] = 1;
        $mem                  = mysql_fetch_array($check_users);
        $_SESSION['userid']   = $mem['id'];
        
        setcookie("userid",$_SESSION['userid'],time()+9900,"/","");
        $ip = ($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
        
        print "<meta http-equiv=\"refresh\" content=\"0;url=http://test.xtraz.cz.cc/\">
    ";
        }
    Should I just access the cookie instead of trying to find the userid session? If so, how would I do that?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    it seems like your database table doesn’t have a column/field named id. you would have to consult your database schema to verify that.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not a PHP issue, moving to MySQL forum.
    As mentioned, the error is quite simple: you have no id within your users table. This is a little bizarre too:
    PHP Code:
    global $c;
    global 
    $ui
    You needn't import a global into global scope. A require/include that is local will automatically generate the variables into the scope for the call, and since its been included into global it will become global.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Dormilich View Post
    it seems like your database table doesn’t have a column/field named id. you would have to consult your database schema to verify that.
    Quote Originally Posted by Fou-Lu View Post
    Not a PHP issue, moving to MySQL forum.
    As mentioned, the error is quite simple: you have no id within your users table. This is a little bizarre too:
    PHP Code:
    global $c;
    global 
    $ui
    You needn't import a global into global scope. A require/include that is local will automatically generate the variables into the scope for the call, and since its been included into global it will become global.
    oh for some reason I thought the table column was id when it was userid. Now it just says,"welcome" instead of welcome, first_name(). And yes I have made sure I am logged in.
    Last edited by elitis; 04-18-2011 at 08:36 PM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Did you correct this one as well?
    PHP Code:
        $_SESSION['userid']   = $mem['id']; 
    That should be $mem['userid'].

    Also, I'm questioning the value of this for you: setcookie("userid",$_SESSION['userid'],time()+9900,"/","");. This cookie only stores the userid, and has a lifetime of 2.75 hours. I can't see where you are using this, but I would hope its not for authentication since I could just push through any userid into a cookie and send you that one and say its a cookie you gave me. Since you can already get this out of session, I'd say that you should just abandon this cookie completely.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    elitis (04-21-2011)

  • #6
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fou-Lu View Post
    Did you correct this one as well?
    PHP Code:
        $_SESSION['userid']   = $mem['id']; 
    That should be $mem['userid'].

    Also, I'm questioning the value of this for you: setcookie("userid",$_SESSION['userid'],time()+9900,"/","");. This cookie only stores the userid, and has a lifetime of 2.75 hours. I can't see where you are using this, but I would hope its not for authentication since I could just push through any userid into a cookie and send you that one and say its a cookie you gave me. Since you can already get this out of session, I'd say that you should just abandon this cookie completely.
    still nothing...

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    DEBUG DEBUG DEBUG.

    Why are so many PHP people afraid of debugging???

    Change this:
    Code:
    $check_users = mysql_query("SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'");
    to this:
    Code:
    $sql ="SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'";
    echo "DEBUG SQL: " . $sql . "<hr>";
    $check_users = mysql_query( $sql ) or die();
    If that doesn't make the problem obvious (and it may not), then copy/paste the SQL into a DB tool such as phpmysqladmin and try it there. What do you get from that?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    elitis (04-21-2011)

  • #8
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Old Pedant View Post
    DEBUG DEBUG DEBUG.

    Why are so many PHP people afraid of debugging???

    Change this:
    Code:
    $check_users = mysql_query("SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'");
    to this:
    Code:
    $sql ="SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'";
    echo "DEBUG SQL: " . $sql . "<hr>";
    $check_users = mysql_query( $sql ) or die();
    If that doesn't make the problem obvious (and it may not), then copy/paste the SQL into a DB tool such as phpmysqladmin and try it there. What do you get from that?
    It echoed this: DEBUG SQL: SELECT * FROM `users` WHERE `email` = '' AND `password` = 'd41d8cd98f00b204e9800998ecf8427e' and I'm not afraid of debugging because until today I never heard of debugging.

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Do you think you've got your query populated with right values, after reading it?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by elitis View Post
    It echoed this: DEBUG SQL: SELECT * FROM `users` WHERE `email` = '' AND `password` = 'd41d8cd98f00b204e9800998ecf8427e' and I'm not afraid of debugging because until today I never heard of debugging.
    Then that's something you need to correct immediately!

    Debugging is the process by which you either manually or through third party software, check where your code is at various points, to check if the results you're getting are sound. In this case, its about echoing out variables just before or after they're changed to see if they're going wrong. As you can see by having echo'ed out this query, $_POST['email'] is coming up blank, meaning either above that echo it was unset or blanked, or that the form did not submit properly. Now you know what the problem is and can tackle it.

    Debugging may seem like a lot of work, and sometimes it is, but its a vital part of programming. Hell, part of debugging is rereading code (like the ones you posted in your OP), and reading it for logic errors (like the other posters did above).
    Last edited by Keleth; 04-20-2011 at 03:50 PM.

  • Users who have thanked Keleth for this post:

    elitis (04-21-2011)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    I'd go so far as to say that you don't know how to program until you know how to debug.

    In professional programming, often over half your time is spent testing and debugging. And sometimes, as when you are changing/fixing code written weeks or months or years before, it can well occupy 90% or more of your time.

    I'd venture to say that I've gotten more than one job because of my experience in testing and debugging, sometimes when I didn't even know the language or system that I would be working on! (To be fair, it has been much more common for me to be hired into such jobs than to be hired into ones where I *do* know the language/system.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Keleth View Post
    Then that's something you need to correct immediately!

    Debugging is the process by which you either manually or through third party software, check where your code is at various points, to check if the results you're getting are sound. In this case, its about echoing out variables just before or after they're changed to see if they're going wrong. As you can see by having echo'ed out this query, $_POST['email'] is coming up blank, meaning either above that echo it was unset or blanked, or that the form did not submit properly. Now you know what the problem is and can tackle it.

    Debugging may seem like a lot of work, and sometimes it is, but its a vital part of programming. Hell, part of debugging is rereading code (like the ones you posted in your OP), and reading it for logic errors (like the other posters did above).
    maybe its just me and I'm not completely understanding but how come the script allows me to log in in the 1st place if $_POST['email'] is coming up blank?

    EDIT: Well, now I feel like I just wasted your time... (which I promised myself I'd try not to do). All, I had to do was add an session_start() in the auth page. (page that actually processes the login page data.) Thanks for all your help. And thank you Old Pedant for teaching me about debugging. Now, I'm one step closer to launching my site
    Last edited by elitis; 04-21-2011 at 11:30 AM.


  •  

    Posting Permissions

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