Problems creating a login system with PHP and MySQLi

0

I'm trying to make a login system and user registration in PHP, but I have several problems only in login.php, the registration is working correctly, but the login is presenting the errors: Warning: mysqli_query () () expects parameter 1 to be mysqli, null given in C: \ wamp \ www \ rifa \ login.php on line 13 and Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, null given in C: \ wamp \ www \ rifa \ login.php on line 14 , my code is just below, does anyone have an idea how I can fix these errors?

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);
$hostname = 'localhost';
$username = 'root';
$password = '12345';
$db = 'rifou';

$connection = new mysqli($hostname, $username, $password);
mysqli_select_db($connection, $db);
$nickname = mysqli_real_escape_string($connection, $_POST['nick']);
$password = mysqli_real_escape_string($connection, $_POST['senha']);
$select_user = "SELECT * FROM usuarios WHERE nick='$nickname' AND senha='$senha'";
$run_user = mysqli_query($con, $select_user);
$check_user = mysqli_num_rows($run_user);
if ($check_user > 0){
    echo "logado";
} else {
   echo "não logado";
}
?>
    
asked by anonymous 19.04.2016 / 02:11

3 answers

2

Note that the following line was declared in your code:

$connection = new mysqli($hostname, $username, $password);

More below you have stated:

$run_user = mysqli_query($con, $select_user);

The variable $ con does not exist because you created it with the name of $ connection. So it's up to you to rename the $ connection variable to $ with or $ con for $ connection.

    
19.04.2016 / 02:34
1
  

Warning: mysqli_query () expects parameter 1 to be mysqli, null given

Your connection is invalid, depending on the error. Your connection variable is $connection and not $con (which is in the mysqli_query() call.

$connection = new mysqli($hostname, $username, $password);
mysqli_select_db($connection, $db);
//Linhas omitidas
$run_user = mysqli_query($con, $select_user); //<--- deve $connection e não $con
    
19.04.2016 / 02:35
-2
$password = mysqli_real_escape_string($connection, $_POST['senha']);
$select_user = "SELECT * FROM usuarios WHERE nick='$nickname' AND senha='$senha'";

You declared the variable as "$ password" and when checking the table you pasted "$ password" to a variable that does not exist

    
26.10.2017 / 15:22