0

I'm writing something like a "e-diary" (for schools, education project) in PHP. I have a problem with database records.

Notice: Undefined variable: name in C:\xampp\htdocs\projects\edziennik\panel.php on line 44

Notice: Undefined variable: surname in C:\xampp\htdocs\projects\edziennik\panel.php on line 44

My code:

if (!empty($_POST['name']) or !empty($_POST['surname'])){
    require('inc/database.php');
    $query = $db->prepare("SELECT * FROM `studentindex`");
    $query->execute();
    $result = $query->fetchAll();
    echo '<table class="table table-striped table-hover ">
            <thead>
                <tr>
                  <th>#</th>
                  <th>Imię</th>
                  <th>Nazwisko</th>
                  <th>Klasa</th>
                </tr>
            </thead><tbody>';
            $row = array();
    foreach ($result as $row){
      if ($row['name'] == $name or $row['surname'] == $surname){
        echo '    <tr>
                    <td>'.$row['id'].'</td>
                    <td>'.$row['name'].'</td>
                    <td>'.$row['surname'].'</td>
                    <td>'.$row['class'].'</td>
                  </tr>';
      }

Exact problem is in foreach, where the if giving that error, in other PHP versions everything was OK (with MySQL). I'm using PDO actually, in database.php is only connection that works fine.

Edit

I have error Notice: Undefined index: id in C:\xampp\htdocs\projects\edziennik\panel.php on line 48 now.

These lines:

echo '<tr>
    <td>'.$row['id'].'</td> // line 48
    <td>'.$row['name'].'</td>
    <td>'.$row['surname'].'</td>
    <td>'.$row['class'].'</td>
</tr>';
Community
  • 1
  • 1
Kacper
  • 111
  • 1
  • 13
  • 3
    $row['name'] == $name or $row['surname'] == $surname //have you assigned value for the variables $name, $surname? – Vigneswaran S Nov 13 '15 at 16:11
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Derek Nov 13 '15 at 16:11

1 Answers1

0

if (!empty($_POST['name']) or !empty($_POST['surname'])){

You have to add variables and assign values:

$name = $_POST['name'];
$surname = $_POST['surname'];
c4pricorn
  • 3,471
  • 1
  • 11
  • 12