2

在 php、mysql 中要 master2。我是 php 新手大约 2 个月

我需要帮助。关于更新表。

我有显示当前表格的页面。当我单击更新按钮更新时,我想要第 1 行,当前表中的数量将减少结果表中的 IdItem。因此,当前表中的等式将像这样数量=数量-数量通行证。所以只有字段项目更改数量结果。现在,当我单击更新时,它会影响表中的所有当前结果。任何解决方案?

“结果”表

+--------+-------------+--------------+-----------+--------+--------------+
| IdItem |  username   |   rentItem   | quantity  | result | quantityPass |
+--------+-------------+--------------+-----------+--------+--------------+
| 84     | FahmiNazirul|   Speaker    |     1     | PASS   |      1       |
+--------+-------------+--------------+-----------+--------+--------------+
| 86     |     Andy    |   Keyboard   |     3     | PASS   |      2       |
+--------+-------------+--------------+-----------+--------+--------------+
| 89     | FahmiNazirul|   Speaker    |     5     | PASS   |      3       |
+--------+-------------+--------------+-----------+--------+--------------+

当前数量“更新”表

+--------+-------------+--------------+ 
|  Id    |    Item     |    quantity  |
+--------+-------------+--------------+ 
|   1    |   Speaker   |     10       |
+--------+-------------+--------------+ 
|   2    |  Keyboard   |     10       | 
+--------+-------------+--------------+ 

表更新.php

$result = mysql_query("SELECT * FROM update where Item = Item");

                echo "<table border='1'>
                <tr>
                <th>Item</th>
                <th>Quantiti</th>
                <th></th>       
                </tr>";

                while($row = mysql_fetch_array($result))
                  {
                  $item= $row['Item'];

                  echo " <tr> ";
                  echo " <td> " . $row['Item'] . " </td> ";
                  echo " <td> " . $row['quantity'] . " </td> ";

                  echo " </tr> ";
                  }
                echo "</table>";

echo " <a href ='quantityupdate.php?Item=$item'  onclick='return Confirm_Box()' >Update</a>";

数量更新.php

$item =$_REQUEST['Item'];


// sending query


mysql_query("UPDATE update ,result SET update.quantity=update.quantity-result.quantityPass where update.Item = result.rentItem")
or die(mysql_error());      

header("Location: update.php");
4

2 回答 2

0
mysql_query("UPDATE update ,result SET update.quantity=update.quantity-result.quantityPass where  result.rentItem='$item' AND update.Item='$item'")
于 2013-03-18T04:24:43.383 回答
0

UPDATE像这样使用JOIN

UPDATE kemaskini AS u
INNER JOIN result AS r ON u.Item = r.rentItem
SET u.quantity = u.quantity - r.quantityPass;

对于Item第一个表中的重复条目,result您可以这样做:

UPDATE kemaskini AS u
INNER JOIN
(
   SELECT rentItem, SUM(quantityPass) AS TotalquantityPass
   FROM result
   GROUP BY rentItem
)  AS r ON u.Item = r.rentItem
SET u.quantity = u.quantity - r.TotalquantityPass;

SQL 小提琴演示

于 2013-03-18T04:14:01.087 回答