Notice: Undefined index

Asked

Viewed 2,078 times

1

I don’t know why you’re making this mistake, I’ve gone over everything.

Notice: Undefined index: first

Notice: Undefined index: last name in C: xampp htdocs project registro.php on line 15

Notice: Undefined index: matricula in C: xampp htdocs projeto registro.php on line 16

Notice: Undefined index: email in C: xampp htdocs projeto registro.php on line 17

Notice: Undefined index: password in C: xampp htdocs project record.php on line 18

<?php 

    $username = 'root';
    $password = '';
    $connection = new PDO( 'mysql:host=localhost;dbname=*****', $username );

    $query = "INSERT INTO usuario (nome, sobrenome, matricula, email, senha) 
          VALUES (:nome, :sobrenome, :matricula, :email, :senha)";

    $statement = $connection->prepare($query);


    $valores = array();
    $valores[':nome'] = $_POST['primeiroNome'];
    $valores[':sobrenome'] = $_POST['sobrenome'];
    $valores[':matricula'] = $_POST['matricula'];
    $valores[':email'] = $_POST['email'];
    $valores[':senha'] = $_POST['senha'];





    $result = $statement->execute($valores);




    ?>

That’s my form right there

<form  method="POST" action ="" name="for">
               <div class="form-group">
                  <div class="form-row">
                     <div class="col-md-6">
                        <label for="primeiroNome">Primeiro nome</label>
                        <input type="text" class="form-control" id="primeiroNome" name="primeiroNome" placeholder="Digite seu primeiro nome">
                     </div>
                     <div class="col-md-6">
                        <label for="Sobrenome">Sobrenome</label>
                        <input type="text" class="form-control" id="Sobrenome" name="sobrenome" placeholder="Digite seu Sobrenome">
                     </div>
                  </div>
               </div>
               <div class="col-md-6" id="matricula">
                  <label for="primeiroNome">Matrícula</label>
                  <input type="text" class="form-control"  name="matricula" placeholder="Digite sua matrícula">
               </div>
               <div class="form-group">
                  <label for="email">E-mail</label>
                  <input type="email" class="form-control" id="email" name="email" placeholder="Digite seu email">
               </div>
               <div class="form-group">
                  <div class="form-row">
                     <div class="col-md-6">
                        <label for="senha">Senha</label>
                        <input type="password" class="form-control" id="senha" name="senha" placeholder="Digite sua senha">
                     </div>
                     <div class="col-md-6">
                        <label for="confirmaSenha">confirmar senha</label>
                        <input type="password" class="form-control" id="confirmaSenha" name="confirmaSenha" placeholder="Confirme sua senha">
                     </div>
                  </div>
               </div>
               <div>
                  <input type="submit" class="btn btn-primary btn-block" href="registro.php" value="Registra-se"/>
               </div>
               <div class="text-center">
                  <a href="#" class="d-block small mt-3">Esqueceu sua senha?</a>
                  <a href="#" class="d-block small mt-3">Login?</a>
               </div>
            </form>
  • 1

    Have you studied the HTTP protocol, which is used on the Web? There are differences between you accessing a page using the GET method and the POST method. You are trying to access data coming in a POST request when it is GET.

  • Your form is displayed and handled on the same page? ( action ="" ) If yes, then you should add one isset, otherwise it will fail the first time you open the page: $valores[':nome'] = $_POST['primeiroNome']; would be $valores[':nome'] = (isset($_POST['primeiroNome']) ? $_POST['primeiroNome'] : ''); or, if you use PHP 7+, like this: $valores[':nome'] = ($_POST['primeiroNome'] ?? '');. I hope I’ve helped!

  • Translating what @Andersoncarloswoss said, in case it wasn’t clear: when you load the page to fill out the form, you have nothing received via POST. And all your PHP code considers that there will always be data received via POST, which only happens when the form is submitted. In short, you need to check if something was posted before running the PHP snippet shown.

  • Thank you, you’ve helped a lot, you’ve solved my problem.

1 answer

1


The problem is because you put the attribute href in the Submit input and does not put the php record. in action form. Remove the add attribute to action and try again. I leave below your edited HTML.

<form method="POST" action="registro.php" name="for">
           <div class="form-group">
              <div class="form-row">
                 <div class="col-md-6">
                    <label for="primeiroNome">Primeiro nome</label>
                    <input type="text" class="form-control" id="primeiroNome" name="primeiroNome" placeholder="Digite seu primeiro nome">
                 </div>
                 <div class="col-md-6">
                    <label for="Sobrenome">Sobrenome</label>
                    <input type="text" class="form-control" id="Sobrenome" name="sobrenome" placeholder="Digite seu Sobrenome">
                 </div>
              </div>
           </div>
           <div class="col-md-6" id="matricula">
              <label for="primeiroNome">Matrícula</label>
              <input type="text" class="form-control"  name="matricula" placeholder="Digite sua matrícula">
           </div>
           <div class="form-group">
              <label for="email">E-mail</label>
              <input type="email" class="form-control" id="email" name="email" placeholder="Digite seu email">
           </div>
           <div class="form-group">
              <div class="form-row">
                 <div class="col-md-6">
                    <label for="senha">Senha</label>
                    <input type="password" class="form-control" id="senha" name="senha" placeholder="Digite sua senha">
                 </div>
                 <div class="col-md-6">
                    <label for="confirmaSenha">confirmar senha</label>
                    <input type="password" class="form-control" id="confirmaSenha" name="confirmaSenha" placeholder="Confirme sua senha">
                 </div>
              </div>
           </div>
           <div>
              <input type="submit" class="btn btn-primary btn-block" value="Registra-se"/>
           </div>
           <div class="text-center">
              <a href="#" class="d-block small mt-3">Esqueceu sua senha?</a>
              <a href="#" class="d-block small mt-3">Login?</a>
           </div>
        </form>

Browser other questions tagged

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