制作一个搜索表单,用户不必输入每个字段


Make a search form where the user doesnt have to enter every field

嗨,我正在制作一个连接到数据库的表单。到目前为止效果不错。它显示信息。然而,我想包括不止一个字段
就像我只在"地点"表格中输入"奥克兰"一样。
我认为isset字段是我陷入困境的部分。我试着包括"&!"或"||"无效。或者可能是我的html。我试图在"isset"中包含一些代码,但浏览器上出现了"没有从服务器检索到数据"错误。因为我只进入了一个领域。我想根据用户输入信息的位置进行操作。顺便说一句,我确实有一个包含在其中,包括其他文件。这只是一个片段。只是我觉得这就是我的错误所在。这是我的html代码。如果你们能帮我想办法的话。

echo "<html>";
echo "<head>";
echo "<title>Your Title Here</title>";
echo "<link rel='"stylesheet'" type='"text/css'" href='"style.css'" />";
echo "</head>";
echo "<body onLoad='"self.focus();document.searchform.search.focus()'">";
echo "<center>";
echo "<br /><form name='"searchform'" method='"GET'" action='"search.php'">";
echo "<input type='"text'" name='"search'" size='"20'" TABINDEX='"1'" />";
echo "<input type='"text'" name='"Institution'" size='"20'" TABINDEX='"1'" />";
echo "<input type='"text'" name='"Location'" size='"20'" TABINDEX='"1'" />";
echo "<input type='"text'" name='"ProjectNotes'" size='"20'" TABINDEX='"1'" />";
echo "<input type='"text'" name='"TotalFunding'" size='"20'" TABINDEX='"1'" />";
echo "<input type='"text'" name='"ActiveYear'" size='"20'" TABINDEX='"1'" />";
echo " <input type='"submit'" value='"Search'" />";
echo "</form>";
//search variable = data in search box or url
if (isset($_GET['search'])) {
    $search = $_GET['search'];
    Investigator($search);
}

研究者功能

function Investigator($search)
{
    $search = trim($search);
    $search = preg_replace('/'s+/', ' ', $search);
//seperate multiple keywords into array space delimited
    $keywords = explode(" ", $search);
//Clean empty arrays so they don't get every row as result
    $keywords = array_diff($keywords, array(
        ""
    ));
//Set the MySQL query
    if ($search == NULL or $search == '%') {
    } else {
        for ($i = 0; $i < count($keywords); $i++) {
            $query = "SELECT * FROM Studies " . "WHERE Investigator LIKE '%$keywords[$i]%'" . " OR      Location LIKE '%$keywords[$i]%'" . " OR TotalFundingAmount LIKE '%$keywords[$i]%'" . " OR  Institution LIKE    '%$keywords[$i]%'" . " ORDER BY Location";
        }
        //Store the results in a variable or die if query fails
        $result = mysql_query($query) or die(mysql_error());
    }
    if ($search == NULL or $search == '%') {
    } else {
        //Count the rows retrived
        $count = mysql_num_rows($result);
        echo $count;
    }
//If search variable is null do nothing, else print it.
    if ($search == NULL) {
    } else {
        echo "You searched for <b><FONT COLOR='"blue'">";
        foreach ($keywords as $value) {
            print "$value ";
        }
        echo "</font></b>";
    }
    echo "<p> </p><br />";
    echo "</center>";
//If users doesn't enter anything into search box tell them to.
    if ($search == NULL) {
        echo "<center><b><FONT COLOR='"red'">Please enter a search parameter to continue.</font></b><br /></center>";
    } elseif ($search == '%') {
        echo "<center><b><FONT COLOR='"red'">Please enter a search parameter to continue.</font></b><br /></center>";
        //If no results are returned print it
    } elseif ($count <= 0) {
        echo "<center><b><FONT COLOR='"red'">Your query returned no results from the database.</font></b><br /></center>";
        //ELSE print the data in a table
    } else {
        //Table header
        echo "<center>";
        echo "</center>";
        //Colors for alternation of row color on results table
        $color1 = "#d5d5d5";
        $color2 = "#e5e5e5";
        //While there are rows, print it.
        while ($row = mysql_fetch_array($result)) {
            //Row color alternates for each row
            $row_color = ($row_count % 2) ? $color1 : $color2;
            //table background color = row_color variable
            echo "<center><table bgcolor=" . $row_color . ">";
            echo "<tr>";
            echo "<td>" . $row['Investigator'] . "</td>";
            echo "<td>" . $row['TotalFundingAmount'] . "</td>";
            echo "<td>" . $row['Institution'] . "</td>";
            echo "</tr>";
            echo "</table></center>";
            $row_count++;
            //end while
        }
        //end if
    }
    echo "</body>";
    echo "</html>";
    if ($search == NULL or $search == '%') {
    } else {
        //clear memory
        mysql_free_result($result);
    }
}
?>

isset()只检查是否定义了变量。empty()可能是您在这种情况下想要使用的,因为它会专门检查表单输入是否为空。

使用它,您可以构造一个将被执行的查询——事实上,它可能不需要单独的函数。我没有看太多你发布的函数代码。但它会变成这样:

假设您有输入字段x、y和z。您希望能够运行一个查询,返回与这些字段中任何一个匹配的所有信息。然后,脚本将为每个输入运行相同的代码模式,$datax是表单中的数据。

if (empty($datax) == false) //If the data is NOT empty...
    {
    $stringx = "column = {$datax}"; //Assign a chunk of a mySQL query requesting matching info to a string.
    }
else //If the data IS empty...
    {
    $stringx = "1"; //Create the same variable, but make it 1.  You'll see why.
    }
//Repeat the above for $datay and $stringy, $dataz and $stringz, etc.  Next is another if to make sure the entire form isn't left blank.
if (empty($datax) == true && empty($datay) == true && empty($dataz) == true)
    {
    //Code to return an "Empty form!" error
    }
else
    {
    $query = "SELECT * FROM table WHERE " . $stringx . " AND " . $stringy . " AND " . $stringz;
    //Continue to run query
    }

这样做的目的是根据数据结果构造一个查询。例如,如果用户搜索USA、一个空白字段和industry(字段分别是位置、天气和类型——同样,所有这些都只是一个例子),那么查询看起来像:

SELECT * FROM table WHERE location = 'USA' AND 1 AND type = 'industrial'

任何保留为空的字段都会插入AND 1语句,而不会影响查询。希望这能澄清一点,如果我能做进一步的编辑来帮助我,请告诉我。