2

php、mysqlでmaster2に。私はPHPで約2ヶ月間新しいです

私は助けが必要です。更新テーブルについて。

現在のテーブルを表示するページがあります。更新ボタンの更新をクリックすると、行1が必要になります。現在のテーブルの数量は、結果テーブルからIdItemだけ減少します。したがって、方程式は現在のテーブルquantity=quantity-quantityPassでこのようになります。したがって、項目品目変更数量結果のみが表示されます。これで、[更新]をクリックすると、テーブルの現在の結果のすべてに影響します。解決策はありますか?

'結果'のテーブル

+--------+-------------+--------------+-----------+--------+--------------+
| 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       | 
+--------+-------------+--------------+ 

tableupdate.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>";

amountupdate.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

このように使用UPDATEJOINます:

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 に答える