我在 PHP 代码中收到“通知:未定义的索引:k”。K 是文本字段的名称,我使用 $_GET[] 方法来获取 k 的值。在下面提到的示例中,即使在提交表单后,我也试图保持该值可用。此代码第一次运行良好,但第二次出现上述错误。
    <form name="keywordquery" method="get" action="keywordsearch.php">
<fieldset class="fieldsetclass"><legend class="legendclass">Search by 
     Keywords</legend>
    <div id="searchbox">
   <input type="text" name="k" value="<?php if(isset($_GET['k'])){echo  
   htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> <input     
    type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png" 
    value="submit" />
</div>
</fieldset>
    </form>
   </div>
  <table id="dataTable" cellpadding="0" cellspacing="0" border="1" style="border-    
   radius:20px; box-shadow: 9px 5px 8px #7E9044;">
  <tbody>    
<?php
        // PAGINATION Code. check if the starting row variable was passed in the 
    URL or not
  if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  //we give the value of the starting row to 0 because nothing was found in URL
  $startrow = 0;
  //otherwise we take the value from the URL
   } else {
  $startrow = (int)$_GET['startrow'];
   }
$k1 = $_GET['k'];
$term = explode(" ", $k1);
$query = "SELECT * FROM data ";
foreach ($term as $each) {
$i++;
    if($i==1)
    {
        $query .= "WHERE keywords LIKE '%$each%' ";
    }
else {
    $query .= " OR WHERE keywords LIKE '%$each%' ";
}
}
$query .= "LIMIT $startrow, 1";
$connection = mysql_connect("xxxx", "xxxxx","");
if(!$connection)
echo "No database connected";
$dbase = mysql_select_db("xxxxxxxx", $connection);
if(!$dbase)
echo "No datatable connected";
$ourquery1 = mysql_query ($query);
if(!$ourquery1)
echo "No query found";
$row1 = mysql_num_rows ($ourquery1);
if ($row1 > 0)
{
    while($result = mysql_fetch_assoc($ourquery1))
    {
        echo "<tr>";
        $title = $result['title'];
        $link = $result['link'];
        $region = $result['region'];
        $sector = $result['sector'];
        $theme = $result['theme'];      
        echo "<td> <a href=$link><h3>$title<h3></a>";
        echo "<h4>Sector: $sector      
                          Theme: $theme   
                 <br> Region: $region </td>";
    }
}
    echo "</tbody>";
       echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+1).'">Next</a>';
       echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-1).'">Prev</a>';