-4

Whenever I try to login on my website it always gives out:

Notice: Undefined variable: conn in P:\xampp\htdocs\CraftedLogin\authenticate.php on line 13

Warning: mysql_query() expects parameter 2 to be resource, null given in P:\xampp\htdocs\CraftedLogin\authenticate.php on line 13

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in P:\xampp\htdocs\CraftedLogin\authenticate.php on line 16 Wrong username or password! Cannot login as gfg

This is my code:

<?php

$username = $_POST['username'];
$password = $_POST['password'];
include("dbConfig.php");

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'";

$rs = mysql_query($sql,$conn);

$num = mysql_num_rows( $rs );

if( $num != 0 )
{ 
  $msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
}
else
{
  $msg = "Wrong username or password! Cannot login as $username";
}
?>

<html>

 <head>
  <title>Log-In</title>
  </head>

  <body>
   <?php echo( $msg ); ?>
  </body>

</html>

Also here is my dbConfig.php:

<?
$host = "localhost";
$user = "CraftedLogin";
$pass = "craftedlogin";
$db = "craftedlogin";
$self =     $_SERVER['PHP_SELF'];
$referer =  $_SERVER['HTTP_REFERER'];
$connection = mysql_connect($host, $user, $pass);
if ( !$conn )
{
echo "Error connecting to database.\n";
}
$rs = @mysql_select_db( $db, $conn ) 
        or die( "Could not select database" );
?>

Can anyone help me?

NOTE: I have tried changing $connection to $conn but it still gives out errors this time with the $num variable but I think I can sort this out quickly.

NEW CODE:

<?php

$username = $_POST['username'];
$password = $_POST['password'];
include("dbConfig.php");

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'";

$rs = @mysql_select_db( $db, $conn );

$numr = mysql_num_rows( $rs );

if( $numr != 0 )
{ 
  $msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
}
else
{
  $msg = "Wrong username or password! Cannot login as $username";
}
?>

<html>

 <head>
  <title>Log-In</title>
  </head>

  <body>
   <?php echo( $msg ); ?>
  </body>

</html>

and dbConfig.php:

<?
$host = "localhost";
$user = "CraftedLogin";
$pass = "craftedlogin";
$db = "craftedlogin";
$self =     $_SERVER['PHP_SELF'];
$referer =  $_SERVER['HTTP_REFERER'];
$conn = mysql_connect($host, $user, $pass);
if ( !$conn )
{
echo "Error connecting to database.\n";
}
$rs = @mysql_select_db( $db, $conn ) 
        or die( "Could not select database" );
?>
MCRocks999
  • 129
  • 1
  • 10

5 Answers5

0

Your connection is $connection while you're using $conn everywhere else.

Igor Yavych
  • 4,078
  • 3
  • 20
  • 41
0

Change

$connection = mysql_connect($host, $user, $pass);

to

$conn = mysql_connect($host, $user, $pass);

That's why it's not working. You're using wrong variable name.

Edit: That is probably what you want:

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'";

$result = mysql_query( $sql, $conn );

$numr = mysql_num_rows( $result );
aksu
  • 5,163
  • 5
  • 22
  • 38
0

In this piece of code:

$rs = @mysql_select_db( $db, $conn ) or die( "Could not select database" );

You are passing $conn, but your connection is defined as $connection. Changing it to:

$rs = @mysql_select_db( $db, $connection) or die( "Could not select database" );

will fix this issue.

Chris W
  • 1,782
  • 15
  • 31
0

fix:

$conn = mysql_connect($host, $user, $pass);
floww
  • 2,008
  • 12
  • 24
0
$connection = mysql_connect($host, $user, $pass);

change to

$conn = mysql_connect($host, $user, $pass);

shafi
  • 310
  • 3
  • 9