2015-08-27 29 views
0

我正在嘗試更新我的網頁搜索功能的代碼,現在它不返回任何東西。我一直在使用它一段時間,並沒有得到任何東西。從表中的mysql數據庫查看查詢數據

這是HTML搜索代碼:

<form method="post" action="words_results1.php"> 
<table align="center"> 
    <tr> 
<td>Keyword</td> 
<td><input type="text" name="Keyword" /></td> 
</tr> 
    <tr> 
    <td>Author</td> 
    <td><input type="text" name="Author" /></td> 
    </tr> 
    <tr> 
    <td valign=bottom>Words Posted<BR />on or before</td> 
    <td valign=top> 
     <table> 
     <tr> 
      <td width="33%">Day</td> 
      <td width="33%">Month</td> 
      <td width="34%">Year</td> 
     </tr> 
     <tr> 
     <td> 
      <select name=Day> 
      <?php 
       echo '<option></option>'; 
       for($count = 1; $count <= 31; ++$count) 
       { 
       echo "<option>$count</option>"; 
       } 
      ?> 
      </select> 
      </td> 
      <td> 
      <select name=Month> 
      <?php 
       echo '<option></option>'; 
       for($count = 1; $count <= 12; $count++) 
       { 
       echo "<option value=$count>".date("M", mktime(0,0,0,$count,1, 2000))."</option>"; 
       } 
      ?> 
      </select> 
      </td> 
      <td> 
      <select name=Year> 
      <?php 
       echo '<option></option>'; 
       for($count = date("Y"); $count >= 1997; $count--) 
       { 
       echo "<option>$count</option>"; 
       } 
      ?> 
      </select> 
      </td> 
     </tr> 
     </table> 
    </td> 
    </tr> 
    <tr> 
    <td colspan=2 align=center> 
     &nbsp;<BR /> 
     <input type="submit" value="Search" /> 
     &nbsp;&nbsp;&nbsp; 
     <input type="submit" name="cancel" value="Cancel" /> 
    </td> 
    </tr> 
</table>  
</form> 

PHP

<?php 

if(isset($_POST['cancel'])) 
    { 
    echo("index.html"); 
    exit; 
    } 

    $qry_string = "SELECT * FROM Words"; 
    $search = ""; 

    if(!empty($Keyword)) 
    { 
    $End_String = "(Word LIKE '%$Keyword%' OR Title LIKE '%$Keyword%')"; 
    $search .="&Keyword=$Keyword"; 
    } 

    if(!empty($Author)) 
    { 
    if(isset($End_String)) 
    { 
     $End_String .= " AND (Author LIKE '%$Author%')"; 
    } 
    else 
    { 
     $End_String = "(Author LIKE '%$Author%')"; 
    } 
    $search .="&Author=$Author"; 
    } 

    if(!empty($Day)) 
    { 
    if(isset($End_String)) 
    { 
     $End_String .= " AND (DAYOFMONTH(Date_Created) = '$Day')"; 
    } 
    else 
    { 
     $End_String = "(DAYOFMONTH(Date_Created) = '$Day')"; 
    } 
    $search .="&Day=$Day"; 
    } 

    if(!empty($Month)) 
    { 
    if(isset($End_String)) 
    { 
     $End_String .= "AND (MONTH(Date_Created) = '$Month')"; 
    } 
    else 
    { 
     $End_String = "(MONTH(Date_Created) = '$Month')"; 
    } 
    $search .="&Month=$Month"; 
    } 

    if(!empty($Year)) 
    { 
    if(isset($End_String)) 
    { 
     $End_String .= " AND (YEAR(Date_Created) = '$Year')"; 
    } 
    else 
    { 
     $End_String = "(YEAR(Date_Created) = '$Year')"; 
    } 
    $search .="&Year=$Year"; 
    } 

    if (!isset($offset)) $offset=0; 

    if(isset($End_String)) 
    { 
    $qry_string = $qry_string." WHERE ".$End_String . " ORDER BY Date_Created DESC LIMIT $offset,101"; 
    } 
    else 
    { 
    $qry_string = $qry_string." ORDER BY Date_Created DESC LIMIT $offset,101"; 
    } 

// echo $qry_string . "<P><HR><P>"; 
    $result = mysql_query($qry_string); 
echo mysql_error(); 
?> 

這最後一點是構成表的代碼,我有一個問題就出在這裏的假設,但老實說,我不肯定在這一點

<table style="margin: 5px 15px; 5px 20px;" align="center" bgcolor="#666666" border="0" cellpadding="3" cellspacing="1"> 
    <tbody><tr style="background: #04C1DE; font-family: Verdana; font-weight: bold; font-size: 18px;"> 
     <td style="width: 50%; padding: 5px;"> 
     Word 
    </td> 
     <td style="width: 20%; padding: 5px;"> 
     Author 
     </td> 
     <td style="width: 10%; padding: 5px;"> 
     Date 
     </td> 
     <td>Category</td> 
     <td>Active?</td> 
     <td>&nbsp;</td> 
     <td>&nbsp;</td> 
    </tr> 
    </tbody> 

    </tr> 

    <?php 
    $count = 1; 
    $bgc = 0; 


    while($row = mysql_fetch_array($sql)) 
    { 
    if ($count > 100) break; 
    echo '<tr style="background: '; 
     if ($bgc==0) echo "#FFFFFF"; 
     else echo "#CFEBFD"; 
     $bgc == 0?$bgc=1:$bgc=0; 
    echo ';">'; 
    echo "<td><a href=../../words/display_word.php?ID=$row[ID]>$row[Title]</a></td>"; 
    echo "<td>$row[Author]</td><td>$row[Display_Date]</td><td>$row[category]</td>"; 
    if($row[active]) 
    { 
     echo "<td>YES</td>"; 
    } 
    else 
    { 
     echo "<td>NO</td>"; 
    } 
    echo "<td>$row[link_count]</td>"; 
    if($row[Title] != "") 
    { 
     echo "<td><a href=words_edit.html?ID=$row[ID]>Edit</a></td></tr>"; 
    } 
    else 
    { 
     echo "</tr>"; 
    } 
    $count++; 
    } 
?> 
+0

你有一堆'if(!empty())'你在哪裏檢查變量,但是我沒有看到這些變量是在哪裏定義的。你在哪裏定義'$ Keyword' /'$ Author' /'$ Day' /'$ Month'?僅僅因爲你發佈了一個表單php不會自動將'$ _POST ['Keyword']'值保存到'$ Keyword' – Sean

+0

你得到的錯誤是什麼? – sriharichander

+0

echo「」;很多地方你在echo字符串裏面使用變量而不用「」符號。這裏$ count將被視爲僅字符串。在你的代碼中同樣的錯誤。即使在sql查詢字符串中也是如此。 –

回答

0

看來,你並沒有收集的價值

$Keyword=$_POST['Keyword']; 

並添加,關閉表格標籤以正確顯示錶格格式的結果。

+0

好吧,我添加了$ Keyword = $ _POST ['Keyword'];並輸入了一些新的代碼回聲'
Word:'。$ row ['Title']; \t echo'
作者:'。$ row ['Author'];它現在顯示正確的搜索,但不是按照echo命令顯示在表格中,你知道如何將它插入到表格中,而不是現在顯示的表格中嗎? –

+0

add,關閉表格標籤以正確顯示錶格格式的結果。 – sriharichander