0

login.php

<?php
function login()
{
    $con = mysqli_connect("localhost", "XXX", "XXX") or die('Could not connect to server');
    mysqli_select_db('$con', "store") or die('Could not connect to database');
}
?>

validate.php

Line 10 - ERROR - Notice: Undefined variable: con in C:\wamp\www\store\admin\validate.php

Line 10 - Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\store\admin\validate.php

Line 12 - Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\store\admin\validate.php

<?php
session_start();
include ("../mylibrary/login.php");
login();

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

$query = "SELECT userid, name from admins where userid = '$userid' and password = PASSWORD('$password')";
$result = mysqli_query($con, $query); (**Line 10)

if (mysqli_num_rows($result) == 0)(**Line 12)
{
    echo "<h5>Sorry, your account was not validated.</h5><br>\n";
    echo "<a href=\"admin.php\">Try again</a><br>\n";
} else
{
    $_SESSION['store_admin'] = $userid;
    header("Location: admin.php");
}
?>

I tried to figure out something wrong. Let me know thanks.

Community
  • 1
  • 1
Kristen
  • 1
  • 1
  • Seems like a *variable scope* issue, `login()` doesn't return anything, so the `$con` you create there is just a local variable in that function. You should `return $con;` in the function, and when you call it, put it into a variable, like `$con = login();` – Qirel Aug 17 '16 at 16:25

2 Answers2

1

See this

mysqli_select_db('$con' 
                 ^    ^

Variable don't get parsed in single quotes.

Either remove them or use double " quotes.

Note: Make sure that all your POST arrays contain values and that the form you're using is indeed using a POST method and elements hold their respective name attributes.

You're also open to an SQL injection here.

Use a prepared statement.

References:

Also consider using password_hash() to store your passwords:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You are mixing up local and global variables. $con is not available outside the login function.

If you add global $con; as the first line of the login function, its available outside of the function as well.

Have a look at the php manual: http://php.net/manual/en/language.variables.scope.php

PS: While its not the source of the problem, read Fred's answer too.

B0nk3rZ
  • 26
  • 3