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" );
?>