【发布时间】:2013-08-27 14:38:26
【问题描述】:
我从网络上获得了这些代码,并成功地将它们组合在一起以按我的意愿工作。从一个简单的php搜索开始...
搜索脚本 - http://php.about.com/od/phpwithmysql/ss/php_search.htm
google…mysql 匹配
自己的工作 - 成功尝试区分大小写的过滤器
突出显示搜索词(从这里stackoverflow) - mysql & php search highlighting
感谢在线和 stackoverflow 分享这些代码的人,他们提供了丰富的知识。
现在有了代码,在我得到结果后,我想有一个更新葡萄酒库存的选项。我不知道从哪里开始,也找不到学习的例子。请指导我正确的方式。谢谢。
这是一个完整的代码,供下一个需要该代码的人使用。
<p align="center">Search Red Wines</p></h1>
//search from - http://php.about.com/od/phpwithmysql/ss/php_search.htm
<form name="search" method="post" action="<?= $PHP_SELF ?>">
<table width="720" border="0" align="center"
style="margin-right:auto; margin-left:auto;">
<tr>
<td valign="middle"><label>Search for:
<input type="text" name="find"/></label>
</td>
<!-- <select name="field">
<option VALUE="name">Name</option>
<option VALUE="location">Location</option>
</select> -->
<td valign="middle"> <label>Name<input
type="checkbox" name="field" value="name"></label></td>
<td valign="middle"> <label>Location<input
type="checkbox" name="field" value="location"></label>
<td valign="middle"> <label>Tasting
note<input type="checkbox" name="field" value="tastingnote"></label>
</td>
<td>
<input type="hidden" name="searching" value="yes"/>
<input type="submit" name="search" value="Search"/>
</td>
</tr>
</table>
</form>
<table width="600" border="0" align="center"
style="margin-right:auto; margin-left:auto;">
<tr>
<td>
<?
//This is only displayed if they have submitted the form
$find = $_POST['find'];
$field = $_POST['field'];
$searching = $_POST['searching'];
if ($searching == "yes") {
//echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "") {
echo "<p>You forgot to enter a search term or choose checkboxs";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost", "chadaveg_bon", "TingTong12") or die(mysql_error());
mysql_select_db("chadaveg_wine") or die(mysql_error());
// We preform a bit of filtering
//$find = strtoupper($find); //make all upper case
//$find = ucfirst($find);
$find = strip_tags($find);
$find = trim($find);
//And we remind them what they searched for
$find = mysql_real_escape_string($find);
echo "<br><font size=5>Searched results for:</b> " . $find;
echo "</font><br><br>";
//Now we search for our search term, in the field the user specified
$data = mysql_query("
SELECT * from
(select * FROM reserveredwine WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
union
select * from pinotnoir WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
union
select * from redwines1 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
union
select * from redwines2 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
union
select * from redwines3 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
union
select * from redwines4 WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE)
union
select * from fortifiedandsweetwine WHERE MATCH $field AGAINST('%" . $find . "%' IN BOOLEAN MODE))
a order by grape, name
");
while ($result = mysql_fetch_array($data)) {
//And we display the results
//highlight code - http://php.about.com/od/phpwithmysql/ss/php_search.htm
$explode_criteria = explode(" ", $find);
$highlight = $result['name'] . " " . $result['grape'] . " " . $result['year'] . " " . "- " . "\$" . $result['price'] . "<br>" . " " . $result['location'] . " - " . $result['instock'] . " in stocks"; // capture $result['name'] here
foreach ($explode_criteria as $key) {
//filter case sensitive search words
if ($field == 'location' || $field == 'tastingnote') {
$key = strtolower($key);
} else {
$key = ucfirst($key);
}
// escape the user input
$key2 = preg_quote($key, '/');
// keep affecting $highlight
$highlight = preg_replace("/" . $key2 . "/", "<span style='box-shadow: 0px 0px 8px 3px #888888; background-color: #FF9900; font-weight: bold; padding: 2px' >" . $key . "</span>", $highlight);
//another way to add more result
//echo "<br>";
//echo '<td>' . $result['name'] . '</td>';
//echo '<td>' . $result['price'] . '</td>';
}
echo $highlight;
//add label picture echo "<img src='../" . $result['label image'] ."'>";
echo "<br>";
echo "<br>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches = mysql_num_rows($data);
if ($anymatches == 0) {
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
}
?>
【问题讨论】:
-
那么,你想让我们通过这堆代码来做些什么呢?此外,名为
redwines1、redwines2、redwines3等的表表明数据库设计可能不是最好的。而且我不是 HTML 专家,但&nbsp;&nbsp;&nbsp;&nbsp;看起来也可以有更优雅的解决方案。再说一遍,究竟是什么问题? -
我想在结果显示后更新数据库'instock'。假设表 redwines1 有 Kendall Jackson Merlot。 3瓶现货。在我搜索肯德尔杰克逊并显示结果后,我想要一个将库存更新为 2 瓶的选项。谢谢。
-
我现在成功地让它工作了......但我有一个问题。我在前一个结束时建立了一个新的数据库连接。但是在我更新数据库之前出现了我成功的更新消息。我该如何解决这个问题?