dougong7850 2015-11-13 16:06
浏览 55
已采纳

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

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>';
  • 写回答

1条回答 默认 最新

  • douju7765 2015-11-13 16:09
    关注

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

    You have to add variables and assign values:

    $name = $_POST['name'];
    $surname = $_POST['surname'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch