PHP does not identify html

Asked

Viewed 37 times

-3

When I try to open the php file the following errors appear:

Notice: Undefined index: name in D: Xampp htdocs web2 php conexao.php on line 14

Notice: Undefined index: email in D: Xampp htdocs web2 php conexao.php on line 15

Notice: Undefined index: RG in D: Xampp htdocs web2 php conexao.php on line 16

Notice: Undefined index: senhaconfirm in D: Xampp htdocs web2 php conexao.php on line 17

HTML

<form id="register-form" method="POST" action="/conexao.php">
                    <div class="col-md-6 register-top-grid">
                        <div>
                            <label for="email">E-mail</label>
                            <input type=text id="email" name="email" placeholder="Digite seu e-mail" maxlenght="50"
                                data-min-length="2" data-email-validate>
                        </div>
                        <div>
                            <label for="name">Nome</label>
                            <input type="text" name="nome" id="nome" placeholder="Digite seu nome" data-required
                                data-min-length="3" data-max-length="40">
                        </div>
                        <div>
                            <label for="lastname">RG</label>
                            <input type="text" name="RG" id="RG" placeholder="Digite seu RG" data-required
                                data-only-letters>
                        </div>
                        <a class="news-letter">
                            <input type="checkbox" name="termos" id="agreement">
                            <label for="agreement" id="agreement-label">Eu li e aceito os termos de uso</label>
                            <p><a class="termos-de-uso" href="#">termos de uso</a></p>
                            <br>
                        </a>
                    </div>
                    <div class="col-md-6 register-bottom-grid">
                        <div>
                            <label for="lastname">Senha</label>
                            <input type="password" name="senha" id="senha" placeholder="Digite sua senha"
                                data-password-validate data-required>
                        </div>
                        <div>
                            <label for="passconfirmation">Confirmação de senha</label>
                            <input type="password" name="senhaconfirm" id="senhaconfirm"
                                placeholder="Digite novamente sua senha" data-equal="password">
                        </div>

                        <div class="full-box">
                            <input id="btn-submit" type="submit" value="Registrar">
                        </div>
                    </div>
                </form>

PHP

<?php 

$hostname = "localhost";
$user = "root";
$password = "root";
$database = "cadastros";
$conexao = mysqli_connect($hostname,$user,$password,$database);

if(!$conexao){
    print "Falha na conexão";
}


$nome = $_POST['nome'];
$email = $_POST['email'];
$RG = $_POST['RG'];
$senha = $_POST['senhaconfirm'];

$sql = "insert into usuarios (nome,email,RG,senha) values ('$nome','$email','$RG','$senha')";
$Registrar = mysqli_query($conexao,$sql);

mysqli_close($conexao)
?>

  • you have undefined variables and your PHP is configured to display notices, errors and warnings, either you change the display of notices or you set your variables before using them, or you can also make use of isset($var) to check if the variable was set before using it.

  • You can also place all PHP within this conditional if(isset($_POST) && ! Empty($_POST)){

  • Don’t forget to close with the } key at the end

1 answer

-1

I’m not sure which version of php you’re using, but they’re not actual errors. They’re Notices that are roughly PHP ways to warn that something is not right.
You can remove the error alerts by disabling them in the PHP settings, in the php.ini file or by adding a @ in front of $_POST, but I do not advise. The right thing to do is to deal with mistakes.
Before accessing fields of an array or object check if they exist. Doing so for example:

$nome = isset($_POST) && isset($_POST['nome']) ? $_POST['nome'] : null;
$email = isset($_POST) && isset($_POST['email']) ? $_POST['email'] : null;;
$RG = isset($_POST) && isset($_POST['RG']) ? $_POST['RG'] : null;
$senha = isset($_POST) && isset($_POST['senhaconfirm']) ? $_POST['RG'] : null;
  • Dear Raul, you don’t need isset($_POST) &&, the isset checks any depth of multi-dimensional arrays, besides it would be better to check everything instead of forcing NULL, you could put everything in one isset($_POST[''nome'], $_POST[''email'], $_POST[''RG']). I recommend reading: https://answall.com/a/264191/3635

Browser other questions tagged

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