-1

我是创建搜索表单的新手,下面是我的搜索表单代码:

<h2>Search</h2> 
 <form name="search" method="post" action="search_result2.php">
 Search for: <input type="text" name="find" /> in 
 <Select NAME="field">
 <Option VALUE="testA">A</option>
 <Option VALUE="testB">B</option>
 <Option VALUE="testC">C</option>
 <Option VALUE="testD">D</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>

更新: search_result2.php:

<?php
 //This is only displayed if they have submitted the form 
if (isset($_POST['searching']) && $_POST['searching'] == "yes") 
{ 
echo "<h2>Results</h2><p>"; 
//If they did not enter a search term we give them an error 
if (empty($_POST['find'])) 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
 
 // Otherwise we connect to our Database 
 mysql_connect("host", "username", "passw") or die(mysql_error()); 
 mysql_select_db("testdb") or die(mysql_error()); 
 
 // We preform a bit of filtering 
 $find = strtoupper($_POST['find']); 
 $find = strip_tags($_POST['find']); 
 $find = trim ($_POST['find']); 
 $field = trim ($_POST['field'])
 
 //Now we search for our search term, in the field the user specified 
 $data = mysql_query("SELECT * FROM testtable WHERE upper($field) LIKE'%$find%'"); 
 
 
 //And we display the results 
 while($result = mysql_fetch_array( $data )) 
 { 
 echo $result['testA']; 
 echo " "; 
 echo $result['testB']; 
 echo "<br>"; 
 echo $result['testC']; 
 echo "<br>"; 
 echo $result['testD']; 
 echo "<br>"; 
 echo "<br>"; 
 } 
 
 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 
 
 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?>

========================== 现在发生的事情是我是否输入搜索字符串它会显示以下消息,这正是我的搜索结果代码,

Results:

    "; //If they did not enter a search term we give them an error if ($find == "") { echo "
    
    You forgot to enter a search term";
    exit;
    } // Otherwise we connect to our Database
    mysql_connect("host", "username", "passw") or die(mysql_error());
    mysql_select_db("testdb") or die(mysql_error());
    // We preform a bit of filtering $find = strtoupper($find);
    $find = strip_tags($find);
    $find = trim ($find);
    //Now we search for our search term, in the field the user specified
    $data = mysql_query("SELECT * FROM testtable WHERE upper($field) LIKE'%$find%'");
    //And we display the results
    while($result = mysql_fetch_array( $data )) {
        echo $result['testA'];
        echo " ";
        echo $result['testB'];
        echo "    ";
        echo $result['testC'];
        echo "    ";
        echo $result['testD'];
        echo "    ";
        echo "  ";
    } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query
    
    "; } //And we remind them what they searched for echo "Searched For: " .$find; } ?>
4

2 回答 2

2

确实使用 <?php而不是<?

其他一些建议

if (isset($_POST['searching']) && $_POST['searching'] == "yes") 
{ 
echo "<h2>Results</h2><p>"; 

//If they did not enter a search term we give them an error 
if (empty($_POST['find'])) 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

还建议您剥离标签$field

于 2013-11-12T15:57:26.033 回答
1

不要使用<?insted 的使用<?php short_open_tag可以在服务器上禁用。

于 2013-11-12T15:49:05.217 回答