php - 想要通过product_id搜索并获取表单字段中基于product_id的所有数据

标签 php jquery mysql product inventory

我正在构建一个库存管理系统,但遇到了搜索查询问题。

我的表名为:

  1. 产品
  2. 产品购买
  3. 产品销售

现在,想要通过搜索“product_id”从“products”表中获取产品详细信息,并在相应的 HTML 字段中获取/显示产品详细信息,然后将产品详细信息数据添加/插入到“product_purchases”表中。

同样,想要通过搜索“product_purchase_id”从“product_purchases”表中获取产品详细信息,并在相应的 HTML 字段中获取/显示产品详细信息,然后将产品详细信息数据添加/插入到“product_sales”表中。

问题是 - 我使用 $.post 方法来查询我的产品详细信息,但它不起作用。

这里我刚刚添加了我购买的屏幕截图

Purchase Page

我的购买页面代码:

<form action="" method="post" class="form-search">             
    <div class="input-group">
      <input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search a product by ID">
      <span class="input-group-btn">
        <button name="searchProduct" value="Search" class="btn btn-primary">Search</button>
      </span>
    </div>
</form>


<form action="" method="post">
    <fieldset id="general-settings">
    <legend><h3 class="fields-heading">Item details</h3></legend>
    <?php 
        if (isset($_POST['addPurchase'])){
            $product_id = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_id']));
            $product_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_name']));
            $supplier_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['supplier_name']));
            $brand_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['brand_name']));
            $group_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['group_name']));
            $unit_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_price']));
            $quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['quantity']));
            $bonus_quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['bonus_quantity']));
            $total_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_price']));
            $total_bonus_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_bonus_price']));
            $unit_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_sale_price']));

            if ( empty($product_id) && empty($product_name) && empty($supplier_name) && empty($brand_name) && empty($group_name) && empty($unit_price) && empty($quantity) && empty($bonus_quantity) && empty($total_price) && empty($total_bonus_price) && empty($unit_sale_price) ){
                echo "<p class='text-error'>Star (*) marks are required fields and cannot be empty!</p>";
            }else{
                $insert = $db->insert("purchases", "product_id, product_name, supplier_id, brand_id, group_id, unit_price, quantity, bonus_quantity, total_price, total_bonus_price, unit_sale_price", "'$product_id', '$product_name', '$supplier_name', '$brand_name', '$group_name', '$unit_price', '$quantity', '$bonus_quantity', '$total_price', $total_bonus_price, '$unit_sale_price'");

                if($insert){ ?>
                    <script>
                        alert("Product added successfully!");
                        window.location.assign("inventory-purchase.php");
                    </script>
                <?php
                }else{
                    echo "<p class='text-error'>Product not added!</p>";
                }
            }
        }
    ?>
    <table class="table table-responsive table-settings">
        <tbody>
        <tr>
            <div class="form-group">
                <td><label for="product_id">Product ID <span class="required">*</span></label></td>
                <td>:</td>
                <td><input type="text" class="form-control" name="product_id" id="product_id"></td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="product_name">Name <span class="required">*</span></label></td>
                <td>:</td>
                <td><input type="text" class="form-control" name="product_name" id="product_name"></td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="supplier_name">Supplier <span class="required">*</span></label></td>
                <td>:</td>
                <td>
                    <select class="form-control" name="supplier_name" id="supplier_name">
                        <option>Select a supplier</option>
                        <?php $getsuppliers = $db->select("supplier_id, supplier_name", "suppliers"); 
                            if ($getsuppliers) {
                                while ( $getsplr = $getsuppliers->fetch_assoc() ){
                        ?>
                        <option value="<?php echo $getsplr['supplier_id']; ?>"><?php echo $getsplr['supplier_name']; ?></option>
                        <?php  } }?>
                    </select>
                </td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="brand_name">Brand <span class="required">*</span></label></td>
                <td>:</td>
                <td>
                    <select class="form-control" name="brand_name" id="brand_name">
                        <option>Select a brand</option>                                                                                  <?php $getbrands = $db->select("brand_id, brand_name", "brands"); 
                            if ($getbrands) {
                                while ( $getbrnd = $getbrands->fetch_assoc() ){
                        ?>
                        <option value="<?php echo $getbrnd['brand_id']; ?>"><?php echo $getbrnd['brand_name']; ?></option>
                        <?php  } }?>
                    </select>
                </td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="group_name">Group <span class="required">*</span></label></td>
                <td>:</td>
                <td>
                    <select class="form-control" name="group_name" id="group_name">
                        <option>Select a group</option>
                        <?php $getgroups = $db->select("group_id, group_name", "groups"); 
                            if ($getgroups) {
                                while ( $getgrp = $getgroups->fetch_assoc() ){
                        ?>
                        <option value="<?php echo $getgrp['group_id']; ?>"><?php echo $getgrp['group_name']; ?></option>
                        <?php  } }?>
                    </select>
                </td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="unit_price">Unit price <span class="required">*</span></label></td>
                <td>:</td>
                <td>
                    <div class="input-group">
                        <span class="input-group-addon">$</span>
                        <input type="text" class="form-control" name="unit_price" id="unit_price">
                    </div>
                </td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="quantity">Quantity <span class="required">*</span></label></td>
                <td>:</td>
                <td><input type="text" class="form-control" name="quantity" id="quantity"></td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="bonus_quantity">Bonus Quantity</label></td>
                <td>:</td>
                <td><input type="text" class="form-control" name="bonus_quantity" id="bonus_quantity"></td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="total_price">Total price</label></td>
                <td>:</td>
                <td>
                    <div class="input-group">
                        <span class="input-group-addon">$</span>
                        <input readonly type="text" class="form-control" name="total_price" id="total_price">
                    </div>
                </td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="total_bonus_price">Total bonus price</label></td>
                <td>:</td>
                <td>
                    <div class="input-group">
                        <span class="input-group-addon">$</span>
                        <input readonly type="text" class="form-control" name="total_bonus_price" id="total_bonus_price">
                    </div>
                </td>
            </div>
        </tr>
        <tr>
            <div class="form-group">
                <td><label for="unit_sale_price">Unit sale price <span class="required">*</span></label></td>
                <td>:</td>
                <td>
                    <div class="input-group">
                        <span class="input-group-addon">$</span>
                        <input type="text" class="form-control" name="unit_sale_price" id="unit_sale_price">
                    </div>
                </td>
            </div>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" name="addPurchase" value="Add purchase" class="btn btn-primary btn-lg btn-register"></td>
        </tr>
        </tbody>
    </table>
    </fieldset>
</form>

和销售产品页面。

sale Page

我的销售页面的 ocde

<form action="" method="post" class="form-search">             
    <div class="input-group">
      <input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search a product by ID">
      <span class="input-group-btn">
        <button name="searchProduct" value="Search" class="btn btn-primary">Search</button>
      </span>
    </div>
</form>

<?php 
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $product_id = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_id']));
        $product_name = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['product_name']));
        $unit_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['unit_sale_price']));
        $quantity = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['quantity']));
        $total_sale_price = $frmt->sanitize(mysqli_real_escape_string($db->link, $_POST['total_sale_price']));

        if ( empty($product_id) && empty($quantity) ){
            echo "<p class='text-error'>Star (*) marks are required fields and cannot be empty!</p>";
        }else{
            $insert = $db->insert("sales", "product_id, product_name, unit_sale_price, quantity, total_sale_price", "'$product_id', '$product_name', '$unit_price', '$quantity', '$total_sale_price'");

            if($insert){ ?>
                <script>
                    alert("Product sent to invoice successfully!");
                    window.location.assign("inventory-sale.php");
                </script>
            <?php
            }else{
                echo "<p class='text-error'>Product not sent to invoice!</p>";
            }
        }
    }
?> 
<form action="" method="post">
    <fieldset id="general-settings">
        <legend><h3 class="fields-heading">Item details</h3></legend>
        <table class="table table-responsive table-settings">
            <tbody>
            <tr>
                <div class="form-group">
                    <td><label for="product_id">Product ID <span class="required">*</span></label></td>
                    <td>:</td>
                    <td><input type="text" class="form-control" name="product_id" id="product_id" placeholder="Search an item by ID"></td>
                </div>
            </tr>
            <tr>
                <div class="form-group">
                    <td><label for="product_name">Product name</label></td>
                    <td>:</td>
                    <td><input type="text" readonly class="form-control" name="product_name" id="product_name" value=""></td>
                </div>
            </tr>

            <tr>
                <div class="form-group">
                    <td><label for="unit_sale_price">Unit sale price</label></td>
                    <td>:</td>
                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">$</span>
                            <input type="number" readonly class="form-control" name="unit_sale_price" id="unit_sale_price" value="">
                        </div>
                    </td>
                </div>
            </tr>
            <tr>
                <div class="form-group">
                    <td><label for="quantity">Quantity <span class="required">*</span></label></td>
                    <td>:</td>
                    <td><input type="number" class="form-control" name="quantity" id="quantity" placeholder="Enter quantity"></td>
                </div>
            </tr>
            <tr>
                <div class="form-group">
                    <td><label for="total_sale_price">Total price</label></td>
                    <td>:</td>
                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">$</span>
                            <input readonly type="text" disabled class="form-control" name="total_sale_price" id="total_sale_price">
                        </div>
                    </td>
                </div>
            </tr>
            <tr>
                <td colspan="3"><input type="submit" name="sale_invoice" value="Sent to invoice" class="btn btn-primary btn-lg btn-register"></td>
            </tr>
            </tbody>
        </table>
    </fieldset>
</form>

仅供引用:我正在使用 PHP OOP,但没有使用任何框架。

有人可以帮我解决这个问题吗?

最佳答案

尝试使用 ajax 请求和 post 方法来获取数据并填充到表单中并存档下一步

how to use ajax with OOP

它可以帮助您获取数据而无需刷新并填充到表单字段中。对于 PHP 来说也是如此,只需使用 ajax 请求调用您的 Controller ,然后以相同的 OOP 方式从模型的数据库中获取数据,并以 JSON 格式返回数据并在您的字段中使用。

需要更多信息,请随时询问:)

关于php - 想要通过product_id搜索并获取表单字段中基于product_id的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48483998/

相关文章:

javascript - jvectormap 合并两个 map

jquery - 如何使用 jQuery 使用 animate 和逗号来增加数字?

mysql - 如何在 MySQL 中使用变量进行 SELECT?

c# - 如何让所有IP地址远程连接mysql

php - 无法从 $_POST 检索大的发布数据

PHP:如何将实例变量传递给闭包?

php - 创建用户登录系统 : Put logic in Code or Database

javascript - 从远程页面到 Bootstrap Modal 的内容

mysql - 这个更新,选择组合查询线程安全吗?

javascript - php语法有问题吗?