如何在搜索结果后更新instock编号?

时间:2013-08-21 08:06:14

标签: php mysql

我从网上获得了这些代码并成功地将它们组合在一起以便按照我的意愿工作。从简单的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">&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>";
    }
}
?>

1 个答案:

答案 0 :(得分: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";
} 
?>