Piachkata Peev Piachkata Peev - 29 days ago 9
HTML Question

Undefined variable and mysqli errors

I get those errors in my code and i don't know why

ERRORS:


  1. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 28

  2. mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 28

  3. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 29

  4. mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 29

  5. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 31

  6. mysqli_query() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 31

  7. mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /opt/local/apache2/htdocs/SE/index.php on line 32

  8. mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /opt/local/apache2/htdocs/SE/index.php on line 33



here are my files

dbcon.php

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "login";

function getConnected($hostname, $username, $password, $database) {
$con = mysqli_connect($hostname, $username, $password, $database);

if($con->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $con;}?>


index.php



<?php session_start(); ?>
<?php include('dbcon.php');?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="form-wrapper">

<form action="#" method="post">
<h3>Влез в системата</h3>

<div class="form-item">
<input type="text" name="user" required="required" placeholder="Фак. Номер" autofocus required/>
</div>

<div class="form-item">
<input type="password" name="pass" required="required" placeholder="Парола" required/>
</div>

<div class="button-panel">
<input type="submit" class="button" title="Log In" name="login" value="Влез"/>
</div>
</form>
<?php
if (isset($_POST['login']))
{
$username = mysqli_real_escape_string($con, $_POST['user']);
$password = mysqli_real_escape_string($con, $_POST['pass']);

$query = mysqli_query($con, "SELECT * FROM users WHERE password='$password' and username='$username'");
$row = mysqli_fetch_array($query);
$num_row = mysqli_num_rows($query);

if ($num_row > 0)
{
$_SESSION['user_id']=$row['user_id'];
header('location:home.php');

}
else
{
echo 'Invalid Username and Password Combination';
}
}
?>
<div class="reminder">
<p>Not a member? <a href="#">Sign up now</a></p>
<p><a href="#">Forgot password?</a></p>
</div>
</div>
</body>
</html>





I get those errors for the mysqli_query(), mysqli_fetch_array() and mysqli_num_rows(). They all expect parameter 1 to be mysqli_result, null given in.

Answer

In your dbcon.php file you have a function getConnected($hostname, $username, $password, $database) but you are never using it. You should in your code in index.php do something like:

$con = getConnected($hostname, $username, $password, $database);

and it will look like follows:

if (isset($_POST['login'])) {
    $con = getConnected($hostname, $username, $password, $database);
    $username = mysqli_real_escape_string($con, $_POST['user']);
    $password = mysqli_real_escape_string($con, $_POST['pass']);
    // ........ rest of your code

Or you can those lines write out of a function:

$con = mysqli_connect($hostname, $username, $password, $database);

        if($con->connect_error) // should be mysqli_connect_errno()
            die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $con; // without return

so dbcon.php would look like this:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "login";

$con = mysqli_connect($hostname, $username, $password, $database);

if($con->connect_error) // should be mysqli_connect_errno()
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

Also in your function (dbcon.php) checking if connection is established is wrote wrong, it should be like follows:

if (mysqli_connect_errno()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

You are using Procedural style, more informations can be found here

Comments