1

我在 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&nbsp; &nbsp; &nbsp; 
                    &nbsp; &nbsp; &nbsp; Theme: $theme &nbsp; 
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<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>';
4

2 回答 2

0

替换行: $k1 = $_GET['k'];

类似于: $k1 = isset($_GET['k'])? $_GET['k'] : $default_k_value;

于 2013-02-18T03:09:18.467 回答
0

您没有显示完整的表格,因此很难判断出了什么问题,但这里有一个提示。将 $_GET 与 $_REQUEST 交换。

例子:

<?php if(isset($_REQUEST['k'])){echo  
   htmlentities($_REQUEST['k']);} ?>

如果表单使用 POST 方法,则该值将在 $_POST 中。如果表单使用 GET 方法,则该值将在 $_GET 中。但是 $_REQUEST 将包含表单字段,无论表单使用的是 POST 还是 GET。

于 2013-02-18T03:15:41.173 回答