...

View Full Version : Unknown column 'id' in 'where clause'?



elitis
04-18-2011, 12:14 PM
I'm trying to display the user's name by I keep getting this error: Unknown column 'id' in 'where clause'


<?php
session_start();
include('database.php');
include('/home/xtraz/public_html/test/functions/index.php');
if ($_SESSION['loggedin'] = 1) {
echo "Welcome, ";
find_name();
}
?>


<?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'];
}
?>


<?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?

Dormilich
04-18-2011, 01:44 PM
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.

Fou-Lu
04-18-2011, 07:18 PM
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:


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.

elitis
04-18-2011, 09:30 PM
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.


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:


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.

Fou-Lu
04-19-2011, 12:13 AM
Did you correct this one as well?


$_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.

elitis
04-19-2011, 12:45 PM
Did you correct this one as well?


$_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...

Old Pedant
04-20-2011, 12:48 AM
DEBUG DEBUG DEBUG.

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

Change this:

$check_users = mysql_query("SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'");

to this:


$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?

elitis
04-20-2011, 12:08 PM
DEBUG DEBUG DEBUG.

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

Change this:

$check_users = mysql_query("SELECT * FROM `users` WHERE `email` = '" . $_POST["email"] . "' AND `password` = '" . md5($_POST["password"]) . "'");

to this:


$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.

abduraooft
04-20-2011, 12:35 PM
Do you think you've got your query populated with right values, after reading it?

Keleth
04-20-2011, 04:48 PM
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).

Old Pedant
04-20-2011, 08:05 PM
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.)

elitis
04-21-2011, 12:18 PM
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 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum