【问题标题】:Auto Calculate MySQL data price based on 1 input field根据 1 个输入字段自动计算 MySQL 数据价格
【发布时间】:2015-08-05 18:30:46
【问题描述】:

我怎样才能一次更改所有价格而不是一一更改? 我只想在字段中输入一个数字,每个价格都会根据我的公式计算并更新。

所以我有一个数据库,里面有一些价格。 我已经有一个php页面,我可以在里面一一编辑价格。

所以名称和价格值是从我的数据库中加载的。 这是 PHP 文件:

<?php
$objConnect = mysql_connect("localhost","***","***") or die("Error Connect to Database");
$objDB = mysql_select_db("***");
$strSQL = "SELECT * FROM orders";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="562" border="1">
  <tr>
    <th width="201"> <div align="center">Name</div></th>
    <th width="213"> <div align="center">Price</div></th>
    <th width="126"> <div align="center">Edit </div></th>
  </tr>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
    <td><?php echo $objResult["gehalte"];?></td>
    <td><?php echo $objResult["prijs"];?></td>
    <td align="center"><a href="prijsedit.php?id=<?php echo $objResult["id"];?>">Edit</a></td>
  </tr>
<?php
}
?>
</table>
<p>Total price:
  <label for="textfield">:</label>
  <input type="text" name="textfield" id="textfield"> 
  <a href="#">Update</a>
</p>


<?php
mysql_close($objConnect);
?>

如果我按下编辑,它将转到另一个 php 页面,我可以在其中更改价格值并保存它:

但我只想在输入字段中输入 1 个价格,该值将由 * 或 - 自动计算并显示为结果。 因此,如果我输入总价,比如说:2000,那么我希望每个价格自动更改,然后我可以按更新。

这样我就可以给每个名字一个公式,比如:2000-500 = SHOW THIS VALUE 而且我只在总字段中输入了一些数字,所有价格都会自动计算,我不必一一更改所有价格。

像这样:http://i.stack.imgur.com/yM40T.png

编辑 PHP 页面:

<html>
<head>

</head>
<body>
<form action="save.php?id=<?php echo $_GET["id"];?>" name="frmEdit" method="post">
<?php
$objConnect = mysql_connect("localhost","*","*") or die("Error Connect to Database");
$objDB = mysql_select_db("*");
$strSQL = "SELECT * FROM orders WHERE id = '".$_GET["id"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if(!$objResult)
{
    echo "Not found CustomerID=".$_GET["id"];
}
else
{
?>
<table width="540" border="1">
  <tr>
    <th width="161"> <div align="center">CustomerID </div></th>
    <th width="203"> <div align="center">Name</div></th>
    <th width="154"> <div align="center">Price</div></th>
    </tr>
  <tr>
    <td><div align="center"><input type="text" name="txtCustomerID" size="5" value="<?php echo $objResult["id"];?>"></div></td>
    <td><input type="text" name="txtName" size="20" value="<?php echo $objResult["gehalte"];?>"></td>
    <td><input type="text" name="txtEmail" size="20" value="<?php echo $objResult["prijs"];?>"></td>
    </tr>
  </table>
  <input type="submit" name="submit" value="submit">
  <?php
  }
  mysql_close($objConnect);
  ?>
</form>
</body>
</html>

并保存 PHP:

$strSQL = "UPDATE orders SET ";
$strSQL .="id = '".$_POST["txtCustomerID"]."' ";
$strSQL .=",gehalte = '".$_POST["txtName"]."' ";
$strSQL .=",prijs = '".$_POST["txtEmail"]."' ";

$strSQL .="WHERE id = '".$_GET["id"]."' ";
$objQuery = mysql_query($strSQL);
if($objQuery)
{
    echo "Save Done.";
    header('Location: edit.php');
}
else
{
    echo "Error Save [".$strSQL."]";
}
mysql_close($objConnect);
?>

【问题讨论】:

  • 必须是这样的:i.stack.imgur.com/yM40T.png
  • 只需要用到javascript
  • 看起来像一张桌子。
  • @jewelhuq 谢谢,我已经在想了。我在哪里可以找到我想要的演示或示例?我搜索了 3 天,但没有结果:(
  • 查看 :stackoverflow.com/questions/1443292/… 或尝试在 google 中更改 onchane 文本框值

标签: php mysql


【解决方案1】:

好的,所以我建议使用多个 SQL 语句。

<?php
//declare update value to variable x 
//SQL Statement 1: select first value from database with ID2
//do math for first value using x and set it to new variable y
//SQL Statement 2: Update database with new value y for ID1
//Do the above for each value
?>

我希望这能回答您的问题。如果您需要进一步解释,请告诉我

【讨论】:

    【解决方案2】:

    这是正确的答案

    1. 创建一个新的 .php 文件,并把它放在那里:

      $objConnect = mysql_connect("localhost","***","***") or die("Error Connect to Database");
      $objDB = mysql_select_db("***");
      $strSQL = "SELECT id FROM orders";
      $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
      
      $value = isset ($_POST['inputField']) ? $_POST['inputField'] : '';
      
      if (!empty($value)) {
          while ($row = mysql_fetch_array($objQuery)) {
              $price = calculate_price ($row['id'], $value);
              $strSQL = "UPDATE orders SET prijs =". $price. " WHERE id = ".$row['id']. "";
              $result = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
          }
      
          echo "<script>alert('Update Successful!');</script>";
      }
      

      函数计算价格($id,$value){ $价格 = 0; 开关($id){ // 这里计算 id 0 案例0: $价格=$价值/10; 休息; // 这里计算 id 1 情况1: $价格=$价值/10; 休息; // 这里计算 id 2
      案例2: $价格 = $ 价值 * 2; 休息; // 这里计算 id 3 案例3: $价格 = $价值 + 4; 休息; 默认 : 休息; } 返回$价格; }

      $(文档).ready(函数() { $('#button').click(function(){ var 值 = $('#inputField').val();
              if (value === '') {
                  alert("Please input a value");  
                  return false;
              }
          })
      })
      

    ***** 如果我将整个代码粘贴到代码标签中,Stackoverflow 会有点奇怪**

    【讨论】:

      【解决方案3】:

      您可以在单个 UPDATE 中执行此操作。

      以下是我对公式所做的假设:

      • 它们可以用空格标记
      • 它们有三个标记,例如X(空格)运算符(空格)Y
      • 他们总是将操作符作为中间标记

      这是我用来模拟您的数据的设置(显然您不需要运行这部分):

      CREATE TABLE orders (
        id numeric,
        gehalte varchar(20),
        formula varchar(20),
        prijs numeric
      );
      
      INSERT INTO orders VALUES (1, '8karaat', 'n / 10', 80);
      INSERT INTO orders VALUES (2, '14karaat', 'n - 500', 150);
      INSERT INTO orders VALUES (3, '18 karaat', 'n * 2', 200);
      

      这是一个SELECT 声明,向您展示UPDATE 将做什么:

      SELECT id
      , gehalte
      , formula
      , prijs AS prijs_original
      , SUBSTRING_INDEX(formula, ' ', 1) AS part1
      , SUBSTRING_INDEX(SUBSTRING_INDEX(formula, ' ', 2),' ',-1) AS part2
      , SUBSTRING_INDEX(SUBSTRING_INDEX(formula,' ',-1),' ',1) AS part3
      , CASE SUBSTRING_INDEX(SUBSTRING_INDEX(formula, ' ', 2),' ',-1) 
        WHEN '+' THEN
          SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) + SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
        WHEN '-' THEN
          SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) - SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
        WHEN '/' THEN
          SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) / SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
        WHEN '*' THEN
          SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) * SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
        END AS prijs_calculated
      FROM orders;
      

      结果:

      id  gehalte     formula  prijs  part1   part2   part3   prijs_calculated
      1   8karaat     n / 10   80     n       /       10      200
      2   14karaat    n - 500  150    n       -       500     1500
      3   18 karaat   n * 2    200    n       *       2       4000
      

      SQL 小提琴:http://sqlfiddle.com/#!9/8816c/1

      part1、part2 和 part3 列说明了如何对公式进行标记化。例如,如果公式为“n / 10”,则第一个标记为“n”,第二个标记为“/”,第三个标记为“10”。 在CASE 语句中,计算第二个标记(运算符),并在此基础上执行数学运算:part1(运算符)part3。 如果需要,您可以添加模运算符作为另一种情况,但我认为它不太可能在您的公式中使用。

      所以UPDATE 语句看起来像这样:

      UPDATE orders 
      SET prijs = CASE SUBSTRING_INDEX(SUBSTRING_INDEX(formula, ' ', 2),' ',-1) 
            WHEN '+' THEN
              SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) + SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
            WHEN '-' THEN
              SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) - SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
            WHEN '/' THEN
              SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) / SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
            WHEN '*' THEN
              SUBSTRING_INDEX(REPLACE(formula,'n',2000), ' ', 1) * SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(formula,'n',2000),' ',-1),' ',1)
            END;
      

      SQLFiddle:http://sqlfiddle.com/#!9/2a962/1

      请注意,我没有 WHERE 子句,因为我假设您正在显示和更新所有记录。如果不是这种情况,您需要提供适当的WHERE 子句来限制更新的记录。

      还有一个重要的注意事项。 您确实应该使用准备好的语句和参数化查询来防止 SQL 注入。 在上面的 UPDATE 语句中,2000 是您的表单帖子中的值。您应该使用参数,而不是使用$_POST["theFormField"]。有一个很好的答案展示了如何在这里做到这一点: How can I prevent SQL injection in PHP?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-03
        • 2017-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多