2015-08-27 128 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