1

I'm getting the error on line 23 which is this line:

if (mysql_num_rows($result) > 0) { 

Can you see my code below and see anything I've missed?

<?php
// Include database connection and select database UFPProducts
     include "../shopdb/connection.php";
?>
<?php
//
session_start();
// (2) Collect data from form and save in variables

$username=$_POST['username'];
$password=$_POST['password']; 

// (3) Create query of the form below to search the user table
//   "SELECT * FROM Users WHERE UserName='$username' AND  Password='$password'"

"SELECT * FROM USERS where Username='$username' AND Password='$password'"

// (3) Run query through connection

// (4) Check result of query using code below

// if rows found set authenticated user to the user name entered 
if (mysql_num_rows($result) > 0) { 
$_SESSION["authenticatedUser"] = $username;
// Relocate to the logged-in page
header("Location: loggedon.php");
} 
else
// login failed redirect back to login page with error message
{
$_SESSION["message"] = "Could not connect as $username " ;
header("Location: login.php");
}
?>

Thank you for your time & help

2
  • 3
    Why do you have "SELECT * FROM USERS where Username='$username' AND Password='$password'" floating around arbitrarily in your code? Commented Nov 16, 2012 at 23:27
  • 2
    Following on from @Musa my guess is you forgot to write the rest of the query. P.s. the mysql_* functions are deprecated please look at mysqli or pdo. Commented Nov 16, 2012 at 23:29

2 Answers 2

0

Use this code, this way you will minimize your chances of getting your site hacked through SQL injection.

<?php
// Include database connection and select database UFPProducts
     include "../shopdb/connection.php";
?>
<?php
//
session_start();
// (2) Collect data from form and save in variables

$username=mysql_real_escape_string(htmlentities($_POST['username']));
$password=mysql_real_escape_string(htmlentities($_POST['password']));

// (3) Create query of the form below to search the user table
//   "SELECT * FROM Users WHERE UserName='$username' AND  Password='$password'"

$query = "SELECT * FROM USERS where Username='$username' AND Password='$password'";
$result = mysql_query($query) or die (mysql_error()); 

// (3) Run query through connection

// (4) Check result of query using code below

// if rows found set authenticated user to the user name entered 
if (mysql_num_rows($result) > 0) { 
$_SESSION["authenticatedUser"] = $username;
// Relocate to the logged-in page
header("Location: loggedon.php");
} 
else
// login failed redirect back to login page with error message
{
$_SESSION["message"] = "Could not connect as $username " ;
header("Location: login.php");
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0
"SELECT * FROM USERS where Username='$username' AND Password='$password'";

Put a semicolon at the end and try

5 Comments

Just tried that, then I got this error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent ( /Applications/XAMPP/xamppfiles/htdocs/site/login/loginAction.php on line 7 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/site/login/loginAction.php on line 23 Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/site/shopdb/connection.php:18)/site/login/loginAction.php on line 32
@BobUni session_start(); should come just after the opening php tag
thanks , I've done that and get the following: Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/site/login/loginAction.php on line 24 Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/site/shopdb/connection.php:18) in /Applications/XAMPP/xamppfiles/htdocs/site/login/loginAction.php on line 33
If you are getting the error above, then try to put ob_start(); at the VERY beginning of your code, and I mean before the <html> like <?php ob_start(); ?> <html> after this, you can put all your code and will never see that error again.
If it worked for you, don't forget to vote up otherwise feel free to ask again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.