0

我从网上获得了这些代码,并成功地将它们组合在一起以按我的意愿工作。从一个简单的 php 搜索开始...

搜索脚本 - http://php.about.com/od/phpwithmysql/ss/php_search.htm

google…mysql 匹配

自己的工作 - 成功尝试区分大小写的过滤器

突出显示搜索词(从这里 stackoverflow) - mysql 和 php 搜索突出显示

感谢那些在网上分享这些代码的人,感谢你在 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">&nbsp;&nbsp;&nbsp;&nbsp;<label>Name<input
                        type="checkbox" name="field" value="name"></label></td>
            <td valign="middle">&nbsp;&nbsp;&nbsp;&nbsp;<label>Location<input
                        type="checkbox" name="field" value="location"></label>
            <td valign="middle">&nbsp;&nbsp;&nbsp;&nbsp;<label>Tasting
                    note<input type="checkbox" name="field" value="tastingnote"></label>
            </td>
            <td>&nbsp;&nbsp;&nbsp;
                <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'] . "&nbsp;&nbsp;&nbsp;" . $result['grape'] . "&nbsp;&nbsp;&nbsp;" . $result['year'] . "&nbsp;&nbsp;" . "-&nbsp;&nbsp;" . "\$" . $result['price'] . "<br>" . "&nbsp;&nbsp;&nbsp;&nbsp;" . $result['location'] . "&nbsp;-&nbsp;" . $result['instock'] . "&nbsp;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>";
    }
}
?>
4

1 回答 1

0
echo "<br>";
echo "<br>";
echo "<form name='searchtotal' method='post' action='" . $PHP_SELF . "?id=" . $result['id'] .     "&instock=" . $result['instock'] . "'>
<table width='720' border='0' align='center' style='margin-right:auto; margin-left:auto;'>
<tr>
<td valign='middle'> <label>In stock: <input type='number' name='updatestock'/></label></td>
<td>&nbsp;&nbsp;&nbsp;<input type='hidden' name='searching1' value='yes' />
 <input type='submit' name='searchtotal' value='update stock' /></td></tr></table>
 </form>";
}


 //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>"; 
 } 


 } 
 mysql_connect("localhost", "username", "password") or die(mysql_error()); 
 mysql_select_db("databasename") or die(mysql_error()); 

$updatestock = $_POST['updatestock'];
$id = $_GET['id'];
$instock = $_GET['instock'];

$updatestock = mysql_real_escape_string($updatestock);
$instock = mysql_real_escape_string($instock);
$id = mysql_real_escape_string($id);

$realstock = $instock - $updatestock;


$data2=mysql_query("UPDATE winelist SET instock=$realstock WHERE id='$id'");

if($data2)
{
//How come this part got echo before I update the database?
echo "Database Connected";
}
else
{
echo "error";
} 
?> 
于 2013-08-26T14:55:48.357 回答