Notice: Undefined index: PHP

2

I'm running a code in PHP and it works totally! It complies with what I ask, but it throws me the following error:

  

Notice: Undefined index: name in C: \ xampp \ htdocs \ project \ profile.php   online 12

What I did was a profile of a login that at the moment of accepting the email with which a user registered, automatically shows his name, and does it perfectly, but that error marks me, this is the code:

<?php
include('classes/DB.php');
include('login.php');
$email = "";
 if (isset($_GET['email'])){
    $name = isset($_GET['name']) ? $_GET['name'] : '';
    if(DB::query('SELECT name, email FROM registroalumnos WHERE email=:email', array(':email'=>$_GET['email']))){
              $email = DB::query('SELECT email FROM registroalumnos WHERE email=:email', array(':email'=>$_GET['email']))[0]['email'];
              $name = DB::query('SELECT name FROM registroalumnos', array(':name'=>$_GET['name']))[0]['name'];
    }else {
        die('Usuario no encontrado');
    }
 }
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <title>Perfil</title>
    <meta charset="UTF-8">
</head>
<body>
    <h1>Perfil de <?php echo $name;?></h1>



</body>
</html>

I already checked the code a lot and I do not know why it causes that.

    
asked by Antonio Alejos 03.02.2018 в 03:06
source

5 answers

5

From what I see in the code:

$email = DB::query('SELECT email FROM registroalumnos WHERE email=:email', array(':email'=>$_GET['email']))[0]['email'];
$name = DB::query('SELECT name FROM registroalumnos', array(':name'=>$_GET['name']))[0]['name'];

You are making a query with the email and then with the name to the same table, obviously you only compare if the email exists here:

 if (isset($_GET['email'])){ ... }

Your array $_GET does not contain the index 'name' and therefore gives you that warning.

The second query is redundant and does not actually filter anything, since it is not parameterized.

You probably wanted to do the following:

$datos_usuario = DB::query('SELECT name, email FROM registroalumnos WHERE email=:email', array(':email'=>$_GET['email']));
//mostrar email y nombre
    
answered by 03.02.2018 / 03:27
source
5
Notice: Undefined index: name in C:\xampp\htdocs\project\profile.php on line 12

It seems that name does not exist, it does not have to be null, it may be that the variable is simply empty.

Your code

$name = isset($_GET['name']) ? $_GET['name'] : '';

Test

$name = empty($_GET['name']) ? $_GET['name'] : '';

Or you may be sent with POST

$name = isset($_POST['name']) ? $_POST['name'] : '';

Without HTML, it is difficult to see what really happens

    
answered by 03.02.2018 в 13:09
2

Judging by the error

Notice: Undefined index: name in C:\xampp\htdocs\project\profile.php on line 12

You are indicating that name does not exist in the% $_GET fix, you must verify the name attribute of your html code.

If you put it in edit the answer based on your code.

    
answered by 03.02.2018 в 03:21
1
  

I'm running a code in PHP and it works totally! comply with what   I ask, but I get the following error: Notice: Undefined index: name in C: \ xampp \ htdocs \ project \ profile.php on line 12

Since the other colleagues have given you the correct answer to your question I can only clarify that it is not a "error" as you mention in your question the message thrown by the xampp . "notice" works more like a "warning" that a process or part of your code is not running in the "best way" according to the standard validations that Handles the xampp .

How are they different from a common error ? As the common error stops the process and creates a flaw in your application, the notice is just a warning that would be wise to take into account.

Whenever the notice shows undefined index it means that some variable was not declared prior to its use and also at the moment of using it said variable is empty. Hence the fact that xampp informs you with a notice . That's it, Un Saludo!

    
answered by 04.02.2018 в 23:24
0

It seems to me that if in the isset you ask if it is equal to 0 or to "", you do not care to use get or post. The two are the same, but get shows the data in the browser's text box and IE has a smaller character capacity.

    
answered by 03.02.2018 в 13:36