2013-02-18 103 views
1

我在PHP代碼中獲得「Notice:Undefined index:k」。 K是文本字段的名稱,我使用$ _GET []方法獲取k的值。在下面提到的例子中,我試圖在提交表單之後保持值可用。此代碼第一次運行良好,但第二次它提供了上述錯誤。變量的PHP值未定義

<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>'; 
+0

您是否定義了$ i?此通知出現在哪一行? – martriay 2013-02-18 02:53:33

回答

0

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

類似於: $ k1 = isset($ _ GET ['k'])? $ _GET ['k']:$ default_k_value;

+0

使用isset()後感謝錯誤不再出現,但是當代碼第一次運行它不顯示第一條記錄,當我按下「下一步」超鏈接時,它顯示數據庫中的第二條記錄。 – user2081780 2013-02-18 19:18:01

0

您不會顯示完整的表格,因此很難分辨出什麼是錯誤的,但這是一個提示。用$ _REQUEST交換$ _GET。

實施例:

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

如果窗體使用POST方法,該值將是$ _POST。如果表單使用GET方法,則該值將位於$ _GET中。但$ _REQUEST將包含表單字段,無論表單是使用POST還是GET。

+0

謝謝,現在我已經在代碼的頂部添加了表單標籤。我希望這會幫助你解決我的問題。 – user2081780 2013-02-18 19:15:04