How can I set my variable "u"?

Asked

Viewed 62 times

0

The following error is appearing: Notice: Undefined variable: u in C: Webserver Apache2.2 htdocs posts register.php on line 30 Fatal error: Call to a Member Function connect() on a non-object in C: Webserver Apache2.2 htdocs posts register.php on line 30

I tried in some ways to solve the problem, but it’s not working, as I can solve?

<html lang="pt-br" >
  <head>
  <title>Projeto Login</title>
  <meta charset="UTF-8" />
  <link rel="stylesheet" href="css/login.css" />
  </head>
  <body>
    <div id="corp-form" >
    <form method="POST" >
      <h1>Cadastrar</h1>
      <input type="text" name="nome" placeholder="Nome completo" maxlength="30" />
      <input type="email" name="email" placeholder="Usuário" maxlength="30" />
      <input type="password" name="senha" placeholder="Senha" maxlength="16" />
      <input type="password" name="confSenha" placeholder="Confirmar a senha" maxlength="16" />
      <input type="submit" name="submit" value="Cadastrar" />
    </form>
    </div>
    <?php
    date_default_timezone_set('America/Sao_Paulo');
    if(isset($_POST['nome']))
    {
      $nome = $_POST['nome'];
      $email = addslashes($_POST['email']);
      $senha = addslashes($_POST['senha']);
      $confirmarSenha = addslashes($_POST['confSenha']);
      //verificar se esta preenchido
      if(!empty($nome) && !empty($email) && !empty($senha)
      && !empty($confirmarSenha))
      {
        $u->conectar("projeto_login","localhost","root","");
        if($nome->msgErro == "")
        {
          if($senha == $confirmarSenha)
        {
        if($u->cadastrar($nome,$email,$senha))
        {
            echo "Cadastrado com sucesso! Acesse para entrar.";
          }
          else
          {
              echo "Email já cadastrado!";
          }
        }
        else
        {
          echo "Senha e confirmar senha não correspondem!";
        }
      }
      else
      {
          echo "Erro: ".$u->msgErro;
        }
      }else
      {
          echo "Preencha todos os campos!";

    }
  }
     ?>
  </body>
</html>

From what I understand my variable "u" is not defined, I have defined in other ways, but it did not work.

  • where you declared the "u", I see neither include from another file nor her statement

  • Along with that part $name = $_POST['name']; $email = addslashes($_POST['email']); $password = addslashes($_POST['password']); $confirm = addslashes($_POST['confNow']); I declared it as a set of all these, but it failed. I did not declare it in this code because apparently I am not able to declare it correctly.

  • Has no variable declaration $u in your code. It is in some external file?

  • @Joãovitor I set it in my index.php <?php date_default_timezone_set('America/Sao_paulo'); require_once 'CLASSES/usuarios.php'; $u = new Usuario; ? > <html lang="en" > <head> <title>Project Login</title> <meta charset="UTF-8" /> <link rel="stylesheet" href="css/login.css" /> </head>

2 answers

1

It seems you did not include the class, consequently you did not urge the same. Try the following:

<?php
     require('diretório/da/classe.php');
     u = new Classe;
  ?>    
  <html lang="pt-br" >
      <head>
      <title>Projeto Login</title>
      <meta charset="UTF-8" />
      <link rel="stylesheet" href="css/login.css" />
      </head>
      <body>
        <div id="corp-form" >
        <form method="POST" >
          <h1>Cadastrar</h1>
          <input type="text" name="nome" placeholder="Nome completo" maxlength="30" />
          <input type="email" name="email" placeholder="Usuário" maxlength="30" />
          <input type="password" name="senha" placeholder="Senha" maxlength="16" />
          <input type="password" name="confSenha" placeholder="Confirmar a senha" maxlength="16" />
          <input type="submit" name="submit" value="Cadastrar" />
        </form>
        </div>
        <?php
        date_default_timezone_set('America/Sao_Paulo');
        if(isset($_POST['nome']))
        {
          $nome = $_POST['nome'];
          $email = addslashes($_POST['email']);
          $senha = addslashes($_POST['senha']);
          $confirmarSenha = addslashes($_POST['confSenha']);
          //verificar se esta preenchido
          if(!empty($nome) && !empty($email) && !empty($senha)
          && !empty($confirmarSenha))
          {
            $u->conectar("projeto_login","localhost","root","");
            if($nome->msgErro == "")
            {
              if($senha == $confirmarSenha)
            {
            if($u->cadastrar($nome,$email,$senha))
            {
                echo "Cadastrado com sucesso! Acesse para entrar.";
              }
              else
              {
                  echo "Email já cadastrado!";
              }
            }
            else
            {
              echo "Senha e confirmar senha não correspondem!";
            }
          }
          else
          {
              echo "Erro: ".$u->msgErro;
            }
          }else
          {
              echo "Preencha todos os campos!";

        }
      }
         ?>
      </body>
    </html>

So you can access the class objects. Test this and tell me what you’ve returned.

By the way, you can exchange require() for include().

  • My variable "u" is set in index.php <?php&#xA;date_default_timezone_set('America/Sao_Paulo');&#xA; require_once 'CLASSES/usuarios.php';&#xA; $u = new Usuario;&#xA; ?>&#xA;<html lang="pt-br" >&#xA; <head>&#xA; <title>Projeto Login</title>&#xA; <meta charset="UTF-8" />&#xA; <link rel="stylesheet" href="css/login.css" />&#xA; </head> When I give include or require, it joins the two pages and the login screen and register are on top of each other.

1


<?php
//...
require_once 'CLASSES/Usuario.php';
$u = new Usuario();
//...
?>
<html>
...
</html>

Make sure that in the User.php class you really have it class name Usuario{...

  • I did this and discovered more errors in my users.php Notice: Use of undefined constant name - assumed 'name' in C:\WebServer\Apache2.2\htdocs\mensagens\CLASSES\usuarios.php on line 13 Warning: PDO::__construct(): [2002] Nenhuma conex�o p�de ser feita porque a m�quina de destino as recusou ativamente. (trying to connect via tcp://localhost:3306) in C:\WebServer\Apache2.2\htdocs\mensagens\CLASSES\usuarios.php on line 13 Notice: Trying to get property of non-object in C:\WebServer\Apache2.2\htdocs\mensagens\CLASSES\usuarios.php on line 13 Notice: Trying to get property of non-object in

  • But, thank you, apparently solved my current.

  • The database connection is not working so you are not able to create the object, always try to protect your code with if(isset()) or is_null, or something like this when there is query in the database and there is possibility that the record does not exist, bd connection or unavailability errors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.