获取MySQL记录时PHP未定义变量错误

时间:2015-11-13 16:06:08

标签: php mysql pdo

我写的是像电子日记" (用于学校,教育项目)PHP。我的数据库记录有问题。

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

我的代码:

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>';
      }

确切的问题在foreach中,如果给出该错误,在其他PHP版本中一切正常(使用MySQL)。我实际上是在使用PDO,在database.php中只有连接才能正常工作。

修改

我现在有错误Notice: Undefined index: id in C:\xampp\htdocs\projects\edziennik\panel.php on line 48

这些行:

echo '<tr>
    <td>'.$row['id'].'</td> // line 48
    <td>'.$row['name'].'</td>
    <td>'.$row['surname'].'</td>
    <td>'.$row['class'].'</td>
</tr>';

1 个答案:

答案 0 :(得分:0)

  

if(!empty($ _ POST [&#39; name&#39;])或!empty($ _ POST [&#39; surname&#39;])){

您必须添加变量并指定值:

Properties props = null;
FileInputStream fis = null;
String propFile = "path_to_your_file_with_properties";

try {
    fis = new FileInputStream(propFile);
    props.load(fis)
    props.setProperty("key", "value");
} catch (Exception e) {
    //handle exception here
} finally {
    //close FileInputStream
}