Connecting Tech Pros Worldwide Help | Site Map

Login Form (USERid, Password) getting error

Newbie
 
Join Date: Nov 2006
Posts: 9
#1: Nov 21 '07
hi guyz!
please help me in sorting out error as im unable to find it...
my requirement is there will be userid field and password field and "login" button after clicking button certain validations must be done whether he is an valid user or not (connect to database and check for valid user & password) if he is valid user he must login and goto another php file.....

im using winxp OS / XAMPP Windows Version 1.6.4 !

Problem: im getting errors im sending the errors & code plz sort out

Expand|Select|Wrap|Line Numbers
  1. Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\sunkalpid\login.php:8) in C:\xampp\htdocs\sunkalpid\login.php on line 9
  2.  
  3. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\sunkalpid\login.php:8) in C:\xampp\htdocs\sunkalpid\login.php on line 9
  4.  
  5. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 24

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Untitled Document</title>
  4. </head>
  5.  
  6. <body>
  7.  
  8. <?php
  9. session_start();
  10.  
  11. //Connect to database
  12.  
  13. mysql_connect ( "localhost", "root", "sunkalpid")or die("Could not connect: ".mysql_error());
  14. mysql_select_db("emp") or die(mysql_error());
  15.  
  16.  
  17. $username = $_POST['username'];
  18. $password = md5($_POST['password']);
  19.  
  20. $query = "SELECT * FROM  users WHERE username=’$username’ and password=’$password’ ";
  21.  
  22. $result = mysql_query($query);
  23.  
  24. if (mysql_num_rows($result) != 1) 
  25.     {
  26. $error = "Bad Login";
  27.     include "login.html";
  28.  
  29. } else {
  30.     $_SESSION['username'] = "$username";
  31.     include "registerdisplay.html";
  32. }
  33.  
  34. ?>
  35.  
  36.  
  37. </body>
  38. </html>
  39.  
looking forward ur help ASAP
Regards
Priya.
Member
 
Join Date: Oct 2007
Posts: 83
#2: Nov 21 '07

re: Login Form (USERid, Password) getting error


Hi Priya,
I think the problem is in the first line of php. means u start the session() before. do one think, just cut that protion of session start() from the begining and paste to else part just before u assign the $SESSION['username'] = $username; ok i think it will work.
[PHP]<?php
//session_start(); remove this line from here

//Connect to database

mysql_connect ( "localhost", "root", "sunkalpid")or die("Could not connect: ".mysql_error());
mysql_select_db("emp") or die(mysql_error());


$username = $_POST['username'];
$password = md5($_POST['password']);

$query = "SELECT * FROM users WHERE username=’$username’ and password=’$password’ ";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1)
{
$error = "Bad Login";
include "login.html";

} else {
session_start();
$_SESSION['username'] = "$username";
include "registerdisplay.html";
}

?>[/PHP]


ok try it and reply ..
Newbie
 
Join Date: Nov 2006
Posts: 9
#3: Nov 21 '07

re: Login Form (USERid, Password) getting error


After placing tht session_start();
in else part i got this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 15
Member
 
Join Date: Oct 2007
Posts: 83
#4: Nov 21 '07

re: Login Form (USERid, Password) getting error


This means that your sql query means line no 13
[PHP]$query = "SELECT * FROM users WHERE username=’$username’ and password=’$password’ "; [/PHP]
be sure that the username and password match the value in database. The error is because, the query dont get the result, thats why its giving this error. ok to check the error do one thing,
[PHP]
/// more code
$query = "SELECT * FROM users WHERE username=’$username’ and password=’$password’ ";
$result = mysql_query($query);
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['username']; // its just cheking the query giving the result or not ok ;
.. more code to write..
[/PHP]

when the query get the result, the error will automtically rectified. ok
Quote:

Originally Posted by priyakollu

After placing tht session_start();
in else part i got this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 15

Ranjan kumar Barik's Avatar
Member
 
Join Date: Aug 2007
Posts: 95
#5: Nov 21 '07

re: Login Form (USERid, Password) getting error


Hi priyakollu

The session_start() must be the the 1st statement of the page.

Good day
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#6: Nov 21 '07

re: Login Form (USERid, Password) getting error


Adding to the above post - move the session start to the very begging of that html file i.e. before the <html> before the <!DOCTYPE, before everything!

:)
Ranjan kumar Barik's Avatar
Member
 
Join Date: Aug 2007
Posts: 95
#7: Nov 21 '07

re: Login Form (USERid, Password) getting error


Hi
I have modified your code a little bit.
Please check it out.
Expand|Select|Wrap|Line Numbers
  1. <?
  2. session start();
  3. ?>
  4. <html>
  5. <head>
  6. <title>Untitled Document</title>
  7. </head>
  8. <body>
  9. <?
  10. extract($_POST);
  11. $username = (isset($_POST['username']))?$_POST['username']:'';
  12. $password = (isset($_POST['password']))?$_POST['password']:'';
  13.  
  14. //Connect to database
  15. $conn = mysql_connect ( 'localhost', 'root', 'sunkalpid')or die("Could not connect: ".mysql_error());
  16. mysql_select_db('emp', $conn) or die(mysql_error());
  17.  
  18. $query = "SELECT * FROM  users WHERE username=’$username’ and password=’$password’ ";
  19.  
  20. $res = mysql_fetch_assoc(mysql_query($query, $conn));
  21.  
  22. if (!$res) 
  23.     {
  24. $error = "Bad Login";
  25.     include "login.html";
  26.  
  27. } else {
  28.     $_SESSION['username'] = "$username";
  29.     include "registerdisplay.html";
  30. }
  31.  
  32. ?>
  33.  
  34.  
  35. </body>
  36. </html>
Newbie
 
Join Date: Nov 2006
Posts: 9
#8: Nov 21 '07

re: Login Form (USERid, Password) getting error


after changing the code i still got an error


Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 20
anyone plz help me
Member
 
Join Date: Oct 2007
Posts: 83
#9: Nov 21 '07

re: Login Form (USERid, Password) getting error


Hi priya, just do one think, show ur html file too. ok the problem is at line no 18 ok.
Quote:

Originally Posted by priyakollu

after changing the code i still got an error


Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 20
anyone plz help me

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,745
#10: Nov 21 '07

re: Login Form (USERid, Password) getting error


Hi Priya.

Do not double post your questions. I have merged your two identical threads into this one thread.
Please read the Posting Guidelines before posting!

Also, please use [code] tags when posting your code examples. (See How to ask a question)

[code=php] ...PHP code goes here... [/code]

Thank you.

P.S. I edited out quotes from a couple of replies, where the poster quoted the entire first post. I hope you don't mind. It was making the thread unnecessarily long.
Ranjan kumar Barik's Avatar
Member
 
Join Date: Aug 2007
Posts: 95
#11: Nov 22 '07

re: Login Form (USERid, Password) getting error


Quote:

Originally Posted by priyakollu

after changing the code i still got an error


Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\sunkalpid\login.php on line 20
anyone plz help me

Hi Priya
Please check, If you have mentioned singles quotes ( ' ) arround $username and $password in line 18, i.e. where you are declaring the $query.

Yes, If The fields are numeric, then no need to do that.

Have a gooooooood day.
Reply