簡體   English   中英

使用PHP / jQuery / Ajax完成編輯后,更新數據庫中的值

[英]Update value in the database when done editing using PHP/jQuery/Ajax

我有一張桌子,像這樣:

  Product    SellingPrice  Cost   Profit
 Product 1        49        45      4
 Product 2        54        50      4

當我在利潤欄中輸入值時,將計算出售價。 我從數據庫中獲取了成本值。 我已經完成了使用jQuery進行計算的工作,但是我堅持如何更新數據庫中的計算。 有人可以指導我有關服務器端的實現嗎?

這是我的代碼:

<html>
<head>
    <title>Content Management System</title>    
    <script>
            //calculate the selling price
            $(document).ready(function(){
                $('tr').each(function(){
                var result = 0;
                $(this).find("input[name=cost],input[name=profit]").each(function(){
                    result += (+$(this).val());
                });
                $(this).find("input[name=sellingprice]").val(result).css("background-color", "green");
                });     
            });
    </script>
</head>
<body>
        <table> 
            <tr>
                <td><center>ID</center></td>
                <td><center>Product</center></td>
                <td><center>Selling Price</center></td>
                <td><center>Current Cost</center></td>
                <td><center>Profit</center></td>
            </tr> 

            <?php

            $result = mysql_query("SELECT id, product, cost FROM inventory");
            while ($myrow = mysql_fetch_row($result))
            {
            ?>
                <tr>
                    <td>
                        <?php echo $myrow[0]; ?>
                    </td>
                    <td>
                        <?php echo $myrow[1]; ?>
                    </td>
                    <td>
                        <?php echo "<input type='text' name='sellingprice' size='10' readonly='true'/>"; ?>
                    </td>
                    <td>
                        <?php echo "<input type='text' name='cost' size='10' value='$myrow[2]' readonly='true'/>"; ?>
                    </td>
                    <td>
                        <?php echo "<input type='text' name='profit' size='10' />"; ?>
                    </td>   
                </tr>   
                <?php
            }
                ?>
        </table>
    </center>
</body>
</html>

您將表單標記添加到表行中,提交表單后,您將獲取/驗證傳入數據並通過ID對表運行UPDATE查詢。 另外,您需要更改輸入元素以使用數組: echo "<input type='text' name='sellingprice[".$myrow[0]."]' size='10' readonly='true'/>";

像這樣

更改您的html並創建一個新的php文件來處理您的表單提交。 在php中,通過$ _GET [“”]獲取值並將其插入表中。

<body>
<form name="some_name" id="form1" action="somephpfilename.php">
        <table> 
            <tr>
                <td><center>ID</center></td>
                <td><center>Product</center></td>
                <td><center>Selling Price</center></td>
                <td><center>Current Cost</center></td>
                <td><center>Profit</center></td>
            </tr> 

            <?php

            $result = mysql_query("SELECT id, product, cost FROM inventory");
            while ($myrow = mysql_fetch_row($result))
            {
            ?>
                <tr>
                    <td>
                        <?php echo $myrow[0]; ?>
                    </td>
                    <td>
                        <?php echo $myrow[1]; ?>
                    </td>
                    <td>
                        <?php echo "<input type='text' name='sellingprice' size='10' readonly='true'/>"; ?>
                    </td>
                    <td>
                        <?php echo "<input type='text' name='cost' size='10' value='$myrow[2]' readonly='true'/>"; ?>
                    </td>
                    <td>
                        <?php echo "<input type='text' name='profit' size='10' />"; ?>
                    </td>   
                </tr>   
                <?php
            }
                ?>
        </table>
    </center>
    <input type="submit" name="update" value="update to db">
    <form>
</body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM