Error index, help me please

Asked

Viewed 73 times

-4

I tried everything, I don’t know why you’re making a mistake, PLEASE HELP ME THE MISTAKE IS: ( ! ) Notice: Undefined index: usuario_id in C: wamp64 www full table excluirAdminUserDoador.php on line 2

HTML CODE:

<?php  

session_start();
include('../verifica_login.php');
$nome = $_SESSION['nome'];

 $connect = mysqli_connect("localhost", "root", "", "login"); 
$query ="SELECT * FROM usuariodoador ORDER BY usuario_id DESC";

 $result = mysqli_query($connect, $query);  

 ?>  

 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Donatário</title>  

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
           <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />

           <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>  
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>            
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />-->
      </head>  
      <body>  
           <br /><br />  
           <div class="container">  
                   <h3 align="center">Logins de donatarios</h3><h4>Olá, <?php echo $nome;?>, <a href="tabelaTesteAdmin.php">Ir para doadores</a></h4><a href="menuAdmin.php">voltar</a></h4>  
                <br /> <br> 
                <div class="table-responsive">  
                     <table id="employee_data" class="table table-striped table-bordered">  
                          <thead>  

                               <tr>  
                                 <div class="cell">
                                    <td>RA</td>
                                  </div>
                                 <div class="cell">
                                    <td>Nome</td> 
                                    </div>
                                    <div class="cell">
                                    <td>Senha criptografada</td>  
                                  </div>
                                    <div class="cell">
                                    <td>Data da criação</td>  
                                  </div>
                                  <div class="cell">
                                    <td>Excluir</td>
                                  <div class="cell">  
                                    <td hidden>Editar</td> 
                                  </div> 

                                  </tr>  
                          </thead>  



                          <?php




                          while($row = mysqli_fetch_array($result)){


                               echo '  
                              <div class="row">
                               <tr> 
                               <div class="cell" data-title="Age"> 
                                    <td >'.$row["usuario_id"].'</td>  
                              </div>
                              <div class="cell" data-title="Full Name">
                                    <td>'.$row["usuario"].'</td>
                              </div>
                                <div class="cell" data-title="Full Name">
                                    <td>'.$row["nome"].'</td>
                              </div>
                              <div class="cell" data-title="Job Title">  
                                    <td>'.$row["senha"].'</td>  
                              </div>
                              <div class="cell" data-title="Location">
                                    <td>'.$row["data_cadastro"].'</td>  
                              </div>

                              <div class="cell" data-title="Location">
                             <td>
                              <a href="excluirAdminUserDoador.php?usuario_id='.$row['usuario_id'].'">Excluir
                              </a>
                              </td>
                            </div>





                               </tr>  

                               ';  
                           }
                          ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  
 <script>  
 $(document).ready(function(){  
      $('#employee_data').DataTable();  
 });  
 </script>  

   <script>
  $(document).ready(function(){
      $('#minhaTabela').DataTable({
          "language": {
                "lengthMenu": "Mostrando _MENU_ registros por página",
                "zeroRecords": "Nada encontrado",
                "info": "Mostrando página _PAGE_ de _PAGES_",
                "infoEmpty": "Nenhum registro disponível",
                "infoFiltered": "(filtrado de _MAX_ registros no total)"
            }
        });
  });
  </script>

PHP CODE:

<?php
$usuario_id = $_GET['usuario_id'];
/*$usuario_id = isset($_POST['usuario_id']) ? $_POST['usuario_id'] : '';*/
include("../conexaoExcluirUser.php");

$sql = "DELETE FROM usuariodoador WHERE usuario_id = $usuario_id";

mysqli_query($link, $sql) or die ("Não foi excluido");

echo "<script language='javascript' type='text/javascript'>alert('Excluido com sucesso!');window.location.href='tabelaTesteAdmin.php'</script>";


?>

RELATED CODE:

<?php

$servidor = "localhost";
$usuario = "root";
$senha = "";
$banco = "login";

$link = mysqli_connect($servidor, $usuario, $senha, $banco);

?>
  • 4

    $_GET['usuario_id'] is not defined. Should be?

1 answer

1


First you need to understand what a Notice is and then know what to do to avoid them. Usually it indicates that a variable, array, constant cannot be accessed. But implicitly if it is there it is because something is not being treated correctly.

It’s like PHP is thinking:

"You shouldn’t be doing what you’re doing, but I’ll allow it anyway".

So now we know what is wrong and the error message itself indicates where. Notice: Undefined index: user name (And yet you quote the line where something is going wrong).

You might be wondering, how so the error is in the user_id, if it gets the $_GET['usuario_id']? You see, you’re getting it, yes, but what about $_GET['usuario_id'] has something to pass to the variable? Surely there is the problem...

You can avoid this type of error by simply checking the existence of the variable and values in it with a if:

if (isset($_GET["usuario_id"]) && $_GET["usuario_id"] != '') {
    $usuario_id = $_GET["usuario_id"];
}else{
    echo "Usuario não definido";
    // Demais acoes... Volta para pagina anterior?
}

So you check whether the variable and its index exist, without trying to access its values (which may not exist), because at this point what matters is its existence, not its value.

Reference:

PHP: isset()

  • 3

    Since you will use isset could use something like (which works very well with strings): if (isset($_GET["usuario_id"][0])) { or simply use Empty, example: if (!empty($_GET["usuario_id"])) {

  • Thank you very much for the explanation, clear and objective!

Browser other questions tagged

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