我在PHP代码中收到“Notice:Undefined index: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>';
答案 0 :(得分:0)
替换该行: $ k1 = $ _GET ['k'];
有类似的东西: $ k1 = isset($ _ GET ['k'])? $ _GET ['k']:$ default_k_value;
答案 1 :(得分:0)
你没有显示完整的表格,所以很难说出错是什么,但这里有一个提示。使用$ _REQUEST交换$ _GET。
示例:
<?php if(isset($_REQUEST['k'])){echo
htmlentities($_REQUEST['k']);} ?>
如果表单使用POST方法,则值将在$ _POST中。如果表单使用GET方法,则值将在$ _GET中。但是,$ _REQUEST将包含表单字段,无论表单是使用POST还是GET。