Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 制作一个搜索表单,用户不必输入每个字段_Php_Mysql - Fatal编程技术网

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

Php 制作一个搜索表单,用户不必输入每个字段,php,mysql,Php,Mysql,嗨,我正在制作一个连接到数据库的表单。到目前为止效果很好。它显示信息。但是,我希望包含多个字段。 就像我只是在“地点”表格中输入“奥克兰”一样。 我认为isset字段是我被卡住的部分。我试过包含“&!”或“| |”无效。或者可能是我的html。我试图在“isset”中包含一些代码,但在浏览器上出现了“无法从服务器检索到数据”错误。因为我只输入了一个字段。我想根据用户输入信息的位置来做。顺便说一句,我有一个包含在它包括其他文件。这只是一个片段。只是我认为这就是我的错误所在。 这是我的html代码。

嗨,我正在制作一个连接到数据库的表单。到目前为止效果很好。它显示信息。但是,我希望包含多个字段。
就像我只是在“地点”表格中输入“奥克兰”一样。
我认为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);
}
echo”“;
回声“;
呼应“你的标题在这里”;
回声“;
回声“;
回声“;
回声“;
回声“
”; 回声“; 回声“; 回声“; 回声“; 回声“; 回声“; 回声“; 回声“; //搜索变量=搜索框或url中的数据 如果(isset($\u GET['search'])){ $search=$_GET['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);
    }
}

?>
功能调查员($search)
{
$search=trim($search);
$search=preg_replace('/\s+/',''$search);
//将多个关键字分隔到分隔的数组空间中
$keywords=explode(“,$search”);
//清理空数组,这样它们就不会得到结果中的每一行
$keywords=array_diff($keywords,array)(
""
));
//设置MySQL查询
如果($search==NULL或$search=='%')){
}否则{
对于($i=0;$i
”; 回声“; //若用户并没有在搜索框中输入任何内容,请告诉他们。 如果($search==NULL){ echo“请输入搜索参数以继续。
”; }elseif($search=='%')){ echo“请输入搜索参数以继续。
”; //如果没有返回结果,请打印它 }埃尔塞夫($count)
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、空白字段和industrial(字段分别为location、weather和type——同样,所有字段都只是一个示例),则查询如下所示:

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

任何字段留空都会导致插入AND 1语句,不会影响查询。希望这能稍微澄清一些问题,请告诉我是否可以进行进一步编辑以提供帮助。

是否需要同时搜索多个字段?您不应该将所有HTML包装在echos中。这将使您的HTML更可读,更易于解码。我看这段代码没有错,你应该包括你的完整来源,特别是
没有从服务器检索到数据的部分。
。是的,杰克,我也需要搜索多个字段。你能发布你的
调查者()吗
函数。另外,就像@MathieuImbert所说的,你不应该用echos包装你的HTML。你可以随意进出PHP。嗯,我有点新。你能简单一点把它分解一下吗。谢谢这有帮助吗,或者。。。?