未定义变量:第 115 行 C:\xampp\htdocs\shed\select.php 中的列表

Undefined variable: list in C:\xampp\htdocs\shed\select.php on line 115

它工作得很好..但是当我打开页面时..它总是给我这条消息未定义的变量:在线 C:\xampp\htdocs\shed\select.php 中的列表115

当我提交页面时,它总是给我正确的数据..但总是向我显示未定义的变量 $list

   <?php
  include("db.php");
  if(isset($_POST['submit']))
  {


    $aa= mysql_real_escape_string($_POST['loco_name']);
    $bb=mysql_real_escape_string ($_POST['section']);

    $result=mysql_query("select * from loco_detail Where loco_name= '$aa' AND section= '$bb' ") or die (mysql_error());




      $Count = mysql_num_rows($result); // count the output amount
     if ($Count > 0)
          {
       while($row = mysql_fetch_array($result))
             { 
         $name = $row["loco_name"];
         $section = $row["section"];
         $schedule = $row["schedule"];
         $repair = $row["repair"];
         $action = $row["action"];

         $date = strftime("%b %d, %Y", strtotime($row["date"]));


     $list .="  


     <table id=\"table\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">
     <tr>
     <td id=\"row\">  $name     </td>
     <td id=\"row\">  $schedule  </td>
      <td id=\"row\">  $repair  </td>
      <td id=\"row\">  $action  </td>
      <td id=\"row\">  $date      </td>
        </tr>
     </table>

     ";      


    }
   } else {
     $list = "You have no products listed in your store yet";
   }



     }



     ?>

这里是html代码

        <body>



    <center>
     <fieldset>
    <legend>Select</legend>
    <table>
    <form method="post">
    <tr><td>Loco Number:</td><td><input type="text" name="loco_name"/></td></tr>
     <tr><td>Section:</td><td><select name="section">
     <option value="Mechanical">Mechanical</option>
     <option value="Electrical">Electrical</option>
     </select></td></tr>
     <tr><td><input type="submit" name="submit" value="Submit"></td></tr>
     </form>
     </table>
      </fieldset>
     </center>


     <div >

       <?php echo $list; ?>

       </div>


       </body>

请让我离开这里。

$list前面有一个句点,调用字符串连接。

错误消息说您正在尝试使用尚未 "declared" 的变量。然而,这些修复起来毫无意义,只会导致您编写更多冗余代码。

您可以在 php.ini 中禁用这些,方法是将 error_reporting 更改为 E_ALL & ~E_NOTICE 或将 error_reporting(E_ALL & ~E_NOTICE); 添加到页面顶部

简而言之 shell 您正试图添加一个名为 $list 的现有变量,该变量从未声明过。

字符串连接示例:

您可以在这里阅读更多内容:PHP String Operators

$string = "Hello "; // Declared
$string .= "World";
$string .= "!";
echo $string;

// Outputs "Hello World!"

基本上要解决您的问题,请将您的 if 语句更改为以下内容

if ($Count > 0) {
    $list = ''; 
    while($row = mysql_fetch_array($result)) { 
        $name = $row["loco_name"];
        $section = $row["section"];
        $schedule = $row["schedule"];
        $repair = $row["repair"];
        $action = $row["action"];
        $date = strftime("%b %d, %Y", strtotime($row["date"]));

        ob_start();
        ?>
        <table id="table" border="1" cellpadding="2" cellspacing="2">
         <tr>
            <td class="row"><?=$name?></td>
            <td class="row"><?=$schedule?></td>
            <td class="row"><?=$repair?></td>
            <td class="row"><?=$action?></td>
            <td class="row"><?=$date?></td>
            </tr>
        </table>
        <?
        $list .= ob_get_clean();

        // rest of code here
    }
}