单击搜索按钮时,产品ID和员工ID不会更改

时间:2015-10-09 07:01:06

标签: php sql

我有这个代码作为一个表单。因此,每当我选择产品ID和员工ID时,当我点击搜索时,将显示产品描述,价格(信息来自数据库)。一旦我输入数量,位置和收据号码的其余信息,我将点击提交。所有这些信息将进入我的数据库sql。

但我遇到的问题是,每当我在数据库中搜索产品ID和员工ID时,它都会刷新。因此产品ID和员工ID将返回到原来的状态,这意味着将插入错误的ID。如果点击搜索但仍然从数据库中获取产品描述和价格,我如何使页面不刷新/更改ID?或者如何在刷新时丢失变量(人员ID和产品ID)?

<div id="login-form">
<form method="post">

<?php

    mysql_connect('localhost', 'root', '');
    mysql_select_db('ishop');

    $sql = "SELECT DISTINCT product_id FROM tbl_product";
    $result = mysql_query($sql);
    echo "<table align='center' width='40%' border='0'>";
    echo "<tr>";
    echo "<td width='100'>";


    echo "Product ID: <select name='product_id' class='form-control input-lg'>";
    while ($row = mysql_fetch_array($result))
    {
        echo "<option value='" . $row['product_id'] ."'>" . $row['product_id'] ."</option>";
    }

    echo "</select>";
    echo "</td>";
    echo "</tr>";


    mysql_connect('localhost', 'root', '');
    mysql_select_db('ishop');

    $sql = "SELECT DISTINCT UserName FROM tbl_user";
    $result = mysql_query($sql);
    echo "<table align='center' width='40%' border='0'>";
    echo "<tr>";
    echo "<td width='100'>";


    echo "Staff ID: <select name='UserName' class='form-control input-lg'>";
    while ($row = mysql_fetch_array($result))
    {
        echo "<option value='" . $row['UserName'] ."'>" . $row['UserName'] ."</option>";
    }


    echo "<tr>";
    echo "<td>";
    echo "<input name='lookup' type='submit' id='lookup' value='Search'>";

    echo "</td>";
    echo "</tr>";

    echo "</form>";

if(isset($_POST['lookup']))
{

        $product_id = $_POST['product_id'];


        mysql_connect('localhost', 'root', '');
        mysql_select_db('ishop');


        $sql = "SELECT DISTINCT product_des FROM tbl_product where product_id ='$product_id'";
        $result = mysql_query($sql);

        echo "<table align='center' width='40%' border='0'>";
        echo "<tr>";
        echo "<td width='100'>";

        echo "Product Description: <select name='product_des' class='form-control input-lg'>";
        while ($row = mysql_fetch_array($result))
        {
            echo "<option value='" . $row['product_des'] ."'>" . $row['product_des'] ."</option>";
        }


        echo "</form>";

        echo "</td>";
        echo "</tr>";

}


if(isset($_POST['lookup']))
{

        $product_id = $_POST['product_id'];

        mysql_connect('localhost', 'root', '');
        mysql_select_db('ishop');


        $sql = "SELECT DISTINCT price FROM tbl_product where product_id ='$product_id'";
        $result = mysql_query($sql);

        echo "<table align='center' width='40%' border='0'>";
        echo "<tr>";
        echo "<td width='100'>";

        echo "Price: <select name='price' class='form-control input-lg'>";
        while ($row = mysql_fetch_array($result))
        {
            echo "<option value='" . $row['price'] ."'>" . $row['price'] ."</option>";
        }


        echo "</form>";

        echo "</td>";
        echo "</tr>";

}


if(isset($_POST['lookup']))
{

        $product_id = $_POST['product_id'];

        mysql_connect('localhost', 'root', '');
        mysql_select_db('ishop');

        echo "<table align='center' width='40%' border='0'>";
        echo "<tr>";
        echo "<td width='100'>";

        echo "Quantity: <input type ='text' name='qty' class='form-control input-lg'>";
        echo "</td>";
        echo "</tr>";

}



if(isset($_POST['lookup']))
{

        $product_id = $_POST['product_id'];

        mysql_connect('localhost', 'root', '');
        mysql_select_db('ishop');

        echo "<table align='center' width='40%' border='0'>";
        echo "<tr>";
        echo "<td width='100'>";

        echo "POS: <input type ='text' name='POS' class='form-control input-lg'>";
        echo "</td>";
        echo "</tr>";

}


if(isset($_POST['lookup']))
{

        $product_id = $_POST['product_id'];

        mysql_connect('localhost', 'root', '');
        mysql_select_db('ishop');

        echo "<table align='center' width='40%' border='0'>";
        echo "<tr>";
        echo "<td width='100'>";

        echo "Receipt Number: <input type ='text' name='ReceiptNumber' class='form-control input-lg'>";
        echo "</td>";
        echo "</tr>";

}
?>



<tr>
<td><button type="submit" name="btn-submit">Submit</button></td>
</tr>

</table>
</form>
</div>

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是使用会话或cookie。

使用$_SESSION看起来像这样:

$_SESSION['product_id'] = $product_id;

对于会话,您需要记住通过将session_start();放在页面顶部来启动它。另外需要注意的是,您需要制定一个逻辑,即如果您“自愿”更改产品ID /员工ID,也会更新$_SESSION值。 (这也与使用cookies时有关。)

对于$_COOKIE这样的事情:

setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */