Notice: Undefined variable: name in C: wamp

Asked

Viewed 512 times

-5

<?php

  if(isset($_SERVER['REQUEST_METHOD']) AND $_SERVER['REQUEST_METHOD'] == 'POST'){

    extract($_POST);

    if($nome == '' OR strlen($nome)<4){
        echo 'Insira um nome existente';
    }elseif($sobrenome=='' OR strlen($sobrenome)<6){ 
        echo 'Insira um sobrenome existente';
    }elseif($email==''){
        echo 'Insira seu e-mail';
    }elseif(!preg_match("/^[az0-9_\.\-]+@[az0-9_\.\-}*{a-z0-9_\-]+\.[a-z]{2,4}$/i",$email)){
        echo 'E-mail invalido tente outro';
    }else{

        include('../../sllapsocial/classes/DB.class.php');

        $verificar = DB::getConn()->prepare("SELECT `id` FROM `usuarios` WHERE `email`=?");
        if($verificar->execute(array($email))){
            if($verificar->rowCount()>=1){
         echo 'Este e-mail ja existe';

     }elseif($senha=='' OR strlen($senha)<4){
        echo'Senha fraca Insira mais caracteres';
     }elseif(strtolower($capctha) <> strtolower($_SESSION['capcthaCadastro'])){
         echo 'Codigo errado';
     }else{
         $senhaInsert = sha1($senha);
         $nascimento = "$ano-$mes-$dia";
         $inserir = DB::getConn()->prepare("INSERT INTO `usuarios` SET `email`=?, `senha` =?, `nome`=? `sobrenome`=?, `sexo`=?, `nascimento`=?`cadastro`=NOW()");

         if($inserir->execute(array($nome,$sobrenome,$email,$senhaInsert,$nascimento,$sexo))){
        header('Location: ./');  
           }

        }

       }

    }

    }

  ?>
  • Bacco I am new in this error is the following when I enter the form and submit it appears this variable error and appears 'insert an existing name' will q the table of users is not beating? THANK YOU FOR YOUR ATTENTION

  • Looks like you want to pick up a form field... are doing it correctly: $_POST['nome_do_campo']?

  • 1

    Place the part of the form where the name field is set.

  • He is giving Extract.

  • 4

    Davidjesus @Papacharlie has already kicked off the problem, but it would be nice if you [Edit] the question and add the form that is sending the data as well.

1 answer

4


PHP is responding that the variable was not defined (Undefined variable). Use isset to check if the variable was created.

if( isset( $nome ) )
{
    if( $nome == '' OR strlen( $nome ) < 4 )
    {
        echo 'menor que 4';
    }
}

OBS 1. $nome == '' and $nome === '' have different result according to the type of the variable, see operators.

OBS2. strlen does not return the exact size of the string when it has special characters. Prefer to use mb_strlen to work with strings.


If you want to pick up a form field, use $_POST['nome_do_campo']. EVER use Extract in form data. Imagine the scenario below with a user injecting a <input name="pdo" />

<form method="post">
     <input name="pdo" />
</form>

$PDO = new PDO( ... );
extract($_POST);

You will lose your PDO instance.

  • By default, extract has the second parameter as EXTR_OVERWRITE (that’s what makes it dangerous in these cases). In the case of form data, it would only be indicated if the EXTR_PREFIX_ALL flag combined with a prefix in the third parameter was used. Then all variables could, for example, have the prefix $post_ in each name. There are also other options to consider in case of collisions of variable names, as can be seen here

  • 1

    @Wallace Maxters, I was going to quote the prefix, but Extract on user data is a bad practice! There is an alert on manual. Using it for user inputs is making misuse of function.

  • 1

    you’re right. Even a similar problem (user data passed for variables) was the one of register globals. I use extract in cases where I have greater control of data, as in the framework. Example: extract(Input::only('nome', 'email'))

  • Every care is little with user data. I use Extract in the connection data coming from my config. If the data is known, it really is no problem to use. If I am not mistaken, Laravel uses Extract in the view to create the variables.

  • 1

    Yes, @Papa. In fact, all the frameworks I’ve used to date do this to send data to the view. In this case, the extract is used within a context of a function or method (since the view’s include is usually done within them), and so is more relaxed, as the data will be encapsulated inside, without conflicting with the variables of the global context.

Browser other questions tagged

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