Notice: Undefined variable: connection [closed]

-1

Recently I changed all my code mysql to mysqli . But that gave me a lot of headache. It gives me the following errors:

Notice: Undefined variable: conexao in D:\Programas\wamp64\www\admin\css\header.php on line 44
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\Programas\wamp64\www\admin\css\header.php on line 44
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in D:\Programas\wamp64\www\admin\css\header.php on line 45

Notice: Undefined variable: conexao in D:\Programas\wamp64\www\admin\css\Cadastro.class.php on line 15
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\Programas\wamp64\www\admin\css\Cadastro.class.php on line 15

You can not enter the time to insert.

Currently my codes are in:

index.php
cadastro.php
css / header.php

asked by anonymous 10.10.2017 / 02:38

1 answer

0

When you must have confused the connection variable.

You are using this in line 17 and 18 of your header.php:

$conectar=new DB;
$conectar=$conectar->conectar();

And so on the error line:

$validaremail=mysqli_query($conexao, "SELECT * FROM usuarios WHERE email='$email'");

Try changing the $ connection variable to $ connect as you started there.

should look like this:

$validaremail=mysqli_query($conectar, "SELECT * FROM usuarios WHERE email='$email'");

Its function of class DB connect () that returns return $conexao; , but what counts is the variable that calls this function in the code:

$conectar=$conectar->conectar();

If correcting this error, new errors appear, you edit this question by putting the new errors in the end.

EDITED

Error in the Registration class of the Cadastro.class.php file :

Classes and functions can not access external variables. So line 15 ( $insert=mysqli_query($conexao, "INSERT... ) of your class can not access the variable $conexao , and even if you change to $conectar will not work either.

You need to enter the variable into the class, and I suggest the magic method __construct() something like this:

<?php

   class Cadastro{
        private $conexao;

        public function __construct($varconexao) {
          $this->conexao = $varconexao;
        }

         public function cadastrar($nome, $nickt, $nickp, $email, $face, $senha, $tel1, $tel2){
 ...
   $insert=mysqli_query($this->conexao, "INSERT INTO usuario...
 ...
         }
    }
 ?>

That is, you create a $conexao variable for your class, and when you start the class you assign your external variable into your class using: new Cadastro($sua_variavel_de_conexao); .

So now just fix your header.php in the line you call the function:

     $cadastro = new Cadastro($conectar);
     echo "<div class='flash'>";
     $cadastro->cadastrar($nome, $nickt, $nickp, $email, $face, $senha, $tel1, $tel2);
     echo "</div>";

You already use the $ connect variable for your connection class, I recommend using another variable for the registration, as I did above with $ registration . If you use the same variable as you are currently doing $conectar=new Cadastro; or $conectar=$conectar->cadastrar , you end up overwriting it, and in the future it may give you more headache, depending on how you create your code.     

10.10.2017 / 04:24