0

您好我正在制作一个连接到数据库的表单。到目前为止效果很好。它显示信息。但是,我想包含多个字段。
就像我只是在“位置”表格中输入“奥克兰”一样。
我认为 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);
    }
}

?>
4

1 回答 1

0

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、空白字段和​​工业(字段分别为位置、天气和类型——同样,这只是一个示例),查询如下所示:

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

任何留空的字段都会导致插入 AND 1 语句,而不影响查询。希望这能澄清一点,让我知道是否可以进行进一步的编辑以提供帮助。

于 2013-08-25T03:22:52.260 回答