【发布时间】:2015-01-23 23:15:07
【问题描述】:
我创建了一个包含五个字段的数据库
ValueA
ValueB
ValueC
ValueD
ValueE
我正在尝试制作一个可以按每个单独字段搜索的搜索表单,例如,如果 ValueB 中的值是“蓝色”,请从下拉列表中选择 ValueB,然后输入“蓝色”以打印出所有值在 Blue 所在的那一排。到目前为止,我已经创建了一个名为“findme.html”的 html 文件:
<html>
<head>
<title>Search</title>
</head>
<body bgcolor=#ffffff>
<h2>Search</h2>
<form name="search" method="post" action="findme2.php">
Search for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="ValueA">Value A</option>
<Option VALUE="ValueB">Value B</option>
<Option VALUE="ValueC">Value C</option>
<Option VALUE="ValueD">Value D</option>
<Option VALUE="ValueE">Value E</option>
</Select>
<input type="submit" name="search" value="Search" />
</form>
</body>
</html>
还创建了一个名为“findme2.php”的php文件:
<html>
<head>
<title>Searching through Database Table mytablename</title>
</head>
<body bgcolor=#ffffff>
<?php
include "config.php";
echo "<h2>Search Results:</h2><p>";
if(isset($_POST['search']))
{
$find =$_POST['find'];
}
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// Otherwise we connect to our Database
$username="xxxxxxxx";
$password="xxxxxxxx";
$database="xxxxxx_xxxxxxx";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
// We perform 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
$iname = mysql_query("SELECT * FROM mytablename WHERE upper($field) LIKE '%$find%'")
or die(mysql_error());
//And we display the results
while($result = mysql_fetch_array( $iname ))
{
echo "id :" .$result['ValueA'];
echo "<br> ";
echo "name :".$result['ValueB'];
echo "<br>";
echo "name :".$result['ValueC'];
echo "<br>";
echo "name :".$result['ValueD'];
echo "<br>";
echo "name :".$result['ValueE'];
echo "<br>";
echo "<br>";
}
$anymatches = mysql_num_rows($iname);
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;
?>
</body>
</html>
我相信我的问题出在查询命令上,但我不确定如何调整语法。谁能帮帮我?
【问题讨论】:
-
首先从你的表单标签中删除
name="search",它不应该在那里。另外,表单没有名称。 -
你说的查询命令是什么?
-
我几乎厌倦了发布这个...拜托,don't use
mysql_*functions,它们不再维护,而是officially deprecated。改为了解prepared statements,并使用PDO 或MySQLi。您还想Prevent SQL Injection! -
@JayBlanchard 我已经放弃了自己。
-
@JayBlanchard 我只是在等待 PHP 7 将删除 mysql_* 函数,然后一半的互联网将被破坏,*“溢出”(你明白了吗?)“为什么是mysql_query 未定义?”...很快,我的朋友,很快。
标签: php html mysql database search