PDA

View Full Version : PHP/MySQL Help, please



agent007
July 30th, 2002, 14:56
Okay, I'm making a little "shoutbox" script more as a learning experience than anything else. What I need to do is check if the username and password is correct. I'm pretty sure it's something like this, but it doesn't work.

Here's what I currently have:

$sql = "SELECT COUNT($username) AS num FROM shout_users WHERE username='$nick' AND password = '$password'";
$result=mysql_query($sql);
if($num == 0) {
echo "Please recheck your username and password combination";
} else{
setcookie ("shoutuser", $username,time()+31536000);
echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=$page\">";
}
No matter what I put in the username/password fields, it always goes to the else statement, rendering it useless. :(

Also, this error shows up whenever I try to "login":

Warning: Cannot add header information - headers already sent by (output started at /home/gta3tips/gta3tips.hey.nu/index.php:4) in /home/gta3tips/gta3tips.hey.nu/shoutbox.php on line 79
Line 79 in my code is the setcookie function.

Christopher
July 30th, 2002, 15:08
Well, I may just be tired but I don't see where the '$num' variable is declared - your testing to see if a non-existant variable is 0 - so you should go $result == 0, because $result was holding how many things returned - right? If I had it my way I would go:


$sql = "SELECT * FROM shout_users WHERE username='$nick' AND password = '$password'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
if($num == 0) {
echo "Please recheck your username and password combination";
} else{
setcookie ("shoutuser", $username,time()+31536000);
echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=$page\">";
}

agent007
July 30th, 2002, 17:04
Oh d'oh, I knew I missed something. I try it out and let you know. Thanks, Chris. :)

agent007
July 30th, 2002, 17:10
Okay, now it successfully checks the user/pass combination. But, when I try to set a cookie with the user's username, it says:

Warning: Cannot add header information - headers already sent by (output started at /home/gta3tips/gta3tips.hey.nu/index.php:4) in /home/gta3tips/gta3tips.hey.nu/shoutbox.php on line 80

Any ideas? :(

agent007
July 30th, 2002, 17:11
By the way, line 80 is:

setcookie ("shoutuser", $nick,time()+31536000);

Canuckkev
July 30th, 2002, 17:14
Make sure you haven't echoed or printed anything, and don't have any blank lines (or anything) before the <?

agent007
July 30th, 2002, 17:41
Okay, I've made sure there are no spaces/anything before/after the <? and ?> tags. It still won't work. You can go here (http://gta3tips.hey.nu/test.php) to see the problem. You can login with the username testuser and the password test.

Any help is appreciated! :)

[edit] If you want to see the code, I'm willing to post it up here. :)

spec
July 30th, 2002, 18:01
the problem is you are using a header after you output

remove any echos or other output b4 you use a header

agent007
July 30th, 2002, 18:16
Okay, I managed to fix it! Thanks to spec, Christopher, and Canuckkev! :D