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